Debian Codegraph @

texk/xdvik/dvi-draw.c

raw
1 /* The rest of the code has the following copyright:
2
3 Copyright (c) 1990-2019 Paul Vojta and others
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to
7 deal in the Software without restriction, including without limitation the
8 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 sell copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
19 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22
23 NOTE:
24 xdvi is based on prior work, as noted in the modification history
25 in xdvi.c.
26
27 \*========================================================================*/
28
29 #include "xdvi-config.h"
30 #include "xdvi.h"
31
32 #include <stdarg.h>
33
34 #include <stdlib.h>
35 #include <ctype.h>
36
37 #include <setjmp.h>
38
39 #define USE_HASH
40
41 #include <ctype.h>
42 #include "kpathsea/c-fopen.h"
43 #include "kpathsea/c-stat.h"
44 #include "kpathsea/magstep.h"
45 #include "kpathsea/tex-file.h"
46
47 #include <string.h>
48
49 #include "dvi.h"
50 #include "string-utils.h"
51 #include "util.h"
52 #include "x_util.h"
53 #include "events.h"
54 #include "dvi-init.h"
55 #include "font-open.h"
56 #include "statusline.h"
57 #include "hypertex.h"
58 #include "special.h"
59 #include "my-snprintf.h"
60 #include "kpathsea/tex-file.h"
61 #include "mag.h"
62 #include "message-window.h"
63
64 #include "dvi-draw.h"
65 #include "search-internal.h"
66 #include "encodings.h"
67 #include "pagesel.h"
68 #include "pagehist.h"
69
70 #if FREETYPE
71 /* other freetype includes are already done in dvi-init.h */
72 # include FT_TYPE1_TABLES_H
73 #endif
74
75 #ifdef RGB_ANTI_ALIASING
76 #define TRACE_AA 0
77 #define TRACE_AA1 1
78 #define TRACE_AA3 0
79 #else
80 #define TRACE_AA 0
81 #define TRACE_AA1 0
82 #define TRACE_AA3 0
83 #endif
84
85 #define MY_DEBUG 0
86
87 #if MY_DEBUG
88 # define TRACE_FIND_VERBOSE(x) TRACE_FIND(x)
89 #else
90 # define TRACE_FIND_VERBOSE(x) /* as nothing */
91 #endif
92
93 #if PS && PS_GS
94 #include "psgs.h"
95 #endif /* PS && PS_GS */
96
97 #ifdef DOPRNT /* define this if vfprintf gives you trouble */
98 # define vfprintf(stream, message, args) _doprnt(message, args, stream)
99 #endif
100
101 #define BUF_SIZE 1024
102
103 /*
104 * All calculations are done with shrink factor = 1, so we re-do some
105 * macros accordingly. Many of these are also defined in special.c.
106 */
107
108 #define xpixel_conv(x) ((int) ((x) >> 16))
109 #define xpixel_round(x) ((int) ROUNDUP(x, 1 << 16))
110
111 #define G_PXL_H xpixel_conv(currinf.data.dvi_h)
112 #define G_OFFSET_X (resource.xoffset_int << 16) + (3 << 15)
113 #define G_OFFSET_Y (resource.yoffset_int << 16) + (3 << 15)
114
115 #if PS
116 int scanned_page_ps;
117 int scanned_page_ps_bak;
118 #endif
119
120 #if COLOR
121 int scanned_page_color;
122 #endif
123
124 int scanned_page_reset, scanned_page;
125
126 struct drawinf currinf;
127 Boolean htex_inside_href = False; /* whether we're inside a href */
128
129 Boolean drawing_mag = False;
130
131 static struct frame frame0; /* dummy head of list */
132 #ifdef TEXXET
133 static struct frame *scan_frame; /* head frame for scanning */
134 #endif
135
136 static const char *const reverse_search_helptext =
137 "Forward/reverse search allows you to jump from a point "
138 "in the DVI file to the corresponding location in the .tex source file, and vice versa. "
139 "To make this possible, the .tex file needs to be compiled with source special support. "
140 "This can be done by using either a package like \"srcltx.sty\" or \"srctex.sty\", "
141 "or a command-line switch like \"-src\" for the TeX executable. "
142 "See the xdvi man page (section SOURCE SPECIALS) "
143 "for more information about this.";
144
145 static ubyte dvi_buffer[DVI_BUFFER_LEN];
146 ubyte *G_dvi_buf_ptr = dvi_buffer;
147 static struct frame *current_frame;
148
149 /* Points to drawinf record containing current dvi file location (for update by
150 geom_scan). */
151 static struct drawinf *dvi_pointer_frame = NULL;
152
153 #ifdef TEXXET
154 #define DIR currinf.dir
155 #else
156 #define DIR 1
157 #endif
158
159 /*
160 * Explanation of the following constant:
161 * offset_[xy] << 16: margin (defaults to one inch)
162 * currwin.shrinkfactor << 16: one pixel page border
163 * currwin.shrinkfactor << 15: rounding for pixel_conv
164 */
165 #define OFFSET_X (resource.xoffset_int << 16) + (currwin.shrinkfactor * 3 << 15)
166 #define OFFSET_Y (resource.yoffset_int << 16) + (currwin.shrinkfactor * 3 << 15)
167
168 #if (BMBYTES == 1)
169 bmUnitT bit_masks[] = {
170 0x0, 0x1, 0x3, 0x7,
171 0xf, 0x1f, 0x3f, 0x7f,
172 0xff
173 };
174 #else
175 #if (BMBYTES == 2)
176 bmUnitT bit_masks[] = {
177 0x0, 0x1, 0x3, 0x7,
178 0xf, 0x1f, 0x3f, 0x7f,
179 0xff, 0x1ff, 0x3ff, 0x7ff,
180 0xfff, 0x1fff, 0x3fff, 0x7fff,
181 0xffff
182 };
183 #else /* BMBYTES == 4 */
184 bmUnitT bit_masks[] = {
185 0x0, 0x1, 0x3, 0x7,
186 0xf, 0x1f, 0x3f, 0x7f,
187 0xff, 0x1ff, 0x3ff, 0x7ff,
188 0xfff, 0x1fff, 0x3fff, 0x7fff,
189 0xffff, 0x1ffff, 0x3ffff, 0x7ffff,
190 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff,
191 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff,
192 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
193 0xffffffff
194 };
195 #endif
196 #endif
197
198 #ifdef VMS
199 #define off_t int
200 #endif
201
202 #ifndef SEEK_SET /* if <unistd.h> is not provided (or for <X11R5) */
203 #define SEEK_SET 0
204 #define SEEK_CUR 1
205 #define SEEK_END 2
206 #endif
207
208 static void draw_part(FILE *fp, struct frame *minframe, double current_dimconv);
209
210 static void source_fwd_draw_box(void);
211
212
213
214 /*
215 * X routines.
216 */
217
218 /*
219 * Put a rectangle on the screen.
220 */
221 static void
222 put_rule(int x, int y, unsigned int w, unsigned int h)
223 {
224 if (htex_inside_href)
225 htex_record_position(x, y, w, h);
226 if (x < globals.win_expose.max_x && x + (int)w >= globals.win_expose.min_x && y < globals.win_expose.max_y && y + (int)h >= globals.win_expose.min_y) {
227 if (--globals.ev.ctr == 0) {
228 if (read_events(EV_NOWAIT) & EV_GE_MAG_GONE) {
229 /* fprintf(stderr, "longjmp1!\n"); */
230 longjmp(globals.ev.canit, 1);
231 }
232 }
233 #if COLOR
234 if (fg_active != fg_current)
235 do_color_change();
236 #endif
237 XFillRectangle(DISP, currwin.win,
238 globals.gc.rule,
239 x - currwin.base_x,
240 y - currwin.base_y,
241 w ? w : 1,
242 h ? h : 1);
243 }
244 }
245
246 static void
247 put_bitmap(struct bitmap *bitmap, int x, int y)
248 {
249 if (globals.debug & DBG_BITMAP)
250 printf("X(%d,%d)\n", x - currwin.base_x, y - currwin.base_y);
251 if (htex_inside_href)
252 htex_record_position(x, y, bitmap->w, bitmap->h);
253 if (x < globals.win_expose.max_x && x + (int)bitmap->w >= globals.win_expose.min_x &&
254 y < globals.win_expose.max_y && y + (int)bitmap->h >= globals.win_expose.min_y) {
255 if (--globals.ev.ctr == 0)
256 if (read_events(EV_NOWAIT) & EV_GE_MAG_GONE) {
257 /* fprintf(stderr, "longjmp2!\n"); */
258 longjmp(globals.ev.canit, 1);
259 }
260 #if COLOR
261 if (fg_active != fg_current)
262 do_color_change();
263 #endif
264 G_image->width = bitmap->w;
265 G_image->height = bitmap->h;
266 G_image->data = bitmap->bits;
267 G_image->bytes_per_line = bitmap->bytes_wide;
268 XPutImage(DISP, currwin.win, globals.gc.fore, G_image,
269 0, 0,
270 x - currwin.base_x, y - currwin.base_y, bitmap->w, bitmap->h);
271 if (globals.gc.fore2) {
272 XPutImage(DISP, currwin.win, globals.gc.fore2, G_image,
273 0, 0,
274 x - currwin.base_x, y - currwin.base_y,
275 bitmap->w, bitmap->h);
276 }
277 }
278 }
279
280 #if GREY
281
282 /* Anti-aliasing stuff.
283 The method used here is supersampling of the unshrunk glyph (this
284 also means that no anti-aliasing happens at shrink 1). A sample
285 of the number of bits that are `on' in the unsrunk glyph determines
286 the grey level of the shrunk image.
287 */
288
289 /* Pixel lookup tables for anti-aliasing: These store all possible
290 supersampling values (i.e. number of bits set in the unshrunk
291 image) for the given shrink factor. E.g. at shrink level 2, the
292 size of the pixel table is 4; in other words, 1 pixel in the shrunk
293 image corresponds to 4 pixels in the unshrunk image. Thus, the possible
294 values for shrink level 2 are: black, 1/4 (0x404040), 1/2 (0x808080),
295 3/4 (0xc0c0c0) and white (0xffffff).
296 */
297 static Pixel *pixeltbl;
298 static Pixel *pixeltbl_gc2; /* drawing to globals.gc.fore2 (compare pixmap2_gc2) */
299
300 static void shrink_glyph_grey(struct glyph *);
301
302 static void
303 put_image(struct glyph *g, int x, int y)
304 {
305 XImage *img = g->image2;
306
307 if (htex_inside_href)
308 htex_record_position(x, y, img->width, img->height);
309 if (x < globals.win_expose.max_x && x + img->width >= globals.win_expose.min_x &&
310 y < globals.win_expose.max_y && y + img->height >= globals.win_expose.min_y) {
311 if (--globals.ev.ctr == 0)
312 if (read_events(EV_NOWAIT) & EV_GE_MAG_GONE) {
313 /* fprintf(stderr, "longjmp3!\n"); */
314 longjmp(globals.ev.canit, 1);
315 }
316
317 #if COLOR
318 if (g->fg != fg_current) /* if color change since last use */
319 shrink_glyph_grey(g);
320 else if (fg_active != fg_current) /* if GCs need updating */
321 do_color_change();
322 #endif
323 /* TODO: Can we increase gamma locally to make the inverted text more readable?
324
325 and to draw the background, so something like this:
326 XFillRectangle(DISP, currwin.win, globals.gc.fore, x - currwin.base_x, y - currwin.base_y,
327 (unsigned int)img->width * 2, (unsigned int)img->height * 2);
328
329 test this with color changes!!
330 */
331 /*TEST_DELAY("check point 1 ...")*/
332 XPutImage(DISP, currwin.win, globals.gc.fore, img,
333 0, 0,
334 x - currwin.base_x, y - currwin.base_y,
335 (unsigned int)img->width, (unsigned int)img->height);
336 /*TEST_DELAY("check point 2 ...")*/
337
338 if (globals.gc.fore2 != NULL) {
339 img->data = g->pixmap2_gc2;
340 XPutImage(DISP, currwin.win, globals.gc.fore2, img,
341 0, 0,
342 x - currwin.base_x, y - currwin.base_y,
343 (unsigned int)img->width, (unsigned int)img->height);
344 img->data = g->pixmap2;
345 }
346 /*TEST_DELAY("check point 3 ...")*/
347 }
348 }
349 #endif /* GREY */
350
351 /*
352 * Draw the border of a rectangle on the screen.
353 * This should be replaced by a grey background both in Xaw and Motif
354 * as soon as #470325 is fixed (see also FIXED_FLUSHING_PAGING).
355 */
356 static void
357 draw_border(int x, int y, unsigned int width, unsigned int height, GC ourGC)
358 {
359 --width;
360 --height;
361 XDrawRectangle(DISP, currwin.win, ourGC, x, y, width, height);
362 }
363
364 /* draw the grid */
365 static void
366 put_grid(int x, int y,
367 unsigned int width, unsigned int height, unsigned int unit,
368 GC gc)
369 {
370 int i;
371 float sep;
372 unsigned int tmp;
373
374 --width;
375 --height;
376
377
378 /* draw vertial grid */
379 #define DRAWGRID_VER(gc) for (i = 1; \
380 (tmp = x + (int)(i * sep)) < x + width; \
381 i++) \
382 XFillRectangle(DISP, currwin.win, (gc), \
383 tmp, y, 1, height)
384 /* draw horizontal grid */
385 #define DRAWGRID_HOR(gc) for (i = 1; \
386 (tmp = y + (int)(i * sep)) < y + height; \
387 i++) \
388 XFillRectangle(DISP, currwin.win, (gc), \
389 x, tmp, width, 1)
390
391 if (resource.grid_mode > 2) { /* third level grid */
392 sep = (float)unit / 4.0;
393 DRAWGRID_VER(gc);
394 DRAWGRID_HOR(gc);
395 }
396
397 if (resource.grid_mode > 1) { /* second level grid */
398 sep = (float)unit / 2.0;
399 DRAWGRID_VER(gc);
400 DRAWGRID_HOR(gc);
401 }
402
403 if (resource.grid_mode > 0) { /* first level grid */
404 sep = (float)unit;
405 DRAWGRID_VER(gc);
406 DRAWGRID_HOR(gc);
407 }
408 }
409 #undef DRAWGRID_VER
410 #undef DRAWGRID_HOR
411
412 /*
413 * Byte reading routines for dvi file.
414 */
415
416 #define xtell(fp, pos) (lseek(fileno(fp), 0L, SEEK_CUR) - \
417 (currinf.end - (pos)))
418
419 static ubyte
420 xxone(FILE *fp)
421 {
422 if (currinf.virtual) {
423 ++currinf.pos;
424 return EOP;
425 }
426
427 currinf.end = G_dvi_buf_ptr
428 + read(fileno(fp), (char *)(currinf.pos = G_dvi_buf_ptr), DVI_BUFFER_LEN);
429 return currinf.end > G_dvi_buf_ptr ? *(currinf.pos)++ : EOF;
430 }
431
432 #define xone(fp) (currinf.pos < currinf.end ? *(currinf.pos)++ : xxone(fp))
433
434 static unsigned long
435 xnum(FILE *fp, ubyte size)
436 {
437 long x = 0;
438
439 while (size--)
440 x = (x << 8) | xone(fp);
441 return x;
442 }
443
444 static long
445 xsnum(FILE *fp, ubyte size)
446 {
447 long x;
448
449 #if __STDC__
450 x = (signed char)xone(fp);
451 #else
452 x = xone(fp);
453 if (x & 0x80)
454 x -= 0x100;
455 #endif
456 while (--size)
457 x = (x << 8) | xone(fp);
458 return x;
459 }
460
461 #define xsfour(fp) xsnum(fp, 4)
462
463 static void
464 xskip(FILE *fp, long offset)
465 {
466 currinf.pos += offset;
467 if (!currinf.virtual && currinf.pos > currinf.end)
468 (void)lseek(fileno(fp), (long)(currinf.pos - currinf.end), SEEK_CUR);
469 }
470
471 void
472 dvi_fmt_error(const char *message, ...)
473 {
474
475 va_list args;
476 va_start(args, message);
477 fprintf(stderr, "%s: ", globals.program_name);
478 (void)vfprintf(stderr, message, args);
479 va_end(args);
480 if (currinf.virtual)
481 fprintf(stderr, " in virtual font %s\n", currinf.virtual->fontname);
482 else
483 fprintf(stderr, ", offset %ld\n", (long)xtell(globals.dvi_file.bak_fp, currinf.pos - 1));
484 /* #ifndef NDEBUG */
485 /* xdvi_exit(EXIT_FAILURE); */
486 /* #else */
487 XDVI_ABORT((stderr, "I'll abort now, to help you debugging this."));
488 /* #endif */
489 }
490
491
492 /*
493 * Code for debugging options.
494 */
495
496 static void
497 print_bitmap(struct bitmap *bitmap)
498 {
499 bmUnitT *ptr = (bmUnitT *)bitmap->bits;
500 int x, y, i;
501
502 ASSERT(ptr != NULL, "Invalid bitmap bits");
503
504 printf("w = %d, h = %d, bytes wide = %d\n",
505 bitmap->w, bitmap->h, bitmap->bytes_wide);
506 for (y = 0; y < (int)bitmap->h; ++y) {
507 for (x = bitmap->bytes_wide; x > 0; x -= BMBYTES) {
508 #ifdef WORDS_BIGENDIAN
509 for (i = BMBITS - 1; i >= 0; --i)
510 putchar((*ptr & (1 << i)) ? '@' : '.');
511 #else
512 for (i = 0; i < BMBITS; ++i)
513 putchar((*ptr & (1 << i)) ? '@' : '.');
514 #endif
515 ++ptr;
516 }
517 putchar('\n');
518 }
519 }
520
521 static void
522 print_char(ubyte ch, struct glyph *g)
523 {
524 printf("char %d", ch);
525 if (isprint(ch))
526 printf(" (%c)", ch);
527 putchar('\n');
528 printf("x = %d, y = %d, dvi = %ld\n", g->x, g->y, g->dvi_adv);
529 print_bitmap(&g->bitmap);
530 }
531
532 static const char *dvi_table1[] = {
533 "SET1", "SET2", NULL, NULL, "SETRULE", "PUT1", "PUT2", NULL,
534 NULL, "PUTRULE", "NOP", "BOP", "EOP", "PUSH", "POP", "RIGHT1",
535 "RIGHT2", "RIGHT3", "RIGHT4", "W0", "W1", "W2", "W3", "W4",
536 "X0", "X1", "X2", "X3", "X4", "DOWN1", "DOWN2", "DOWN3",
537 "DOWN4", "Y0", "Y1", "Y2", "Y3", "Y4", "Z0", "Z1",
538 "Z2", "Z3", "Z4"
539 };
540
541 static const char *dvi_table2[] = {
542 "FNT1", "FNT2", "FNT3", "FNT4", "XXX1", "XXX2", "XXX3", "XXX4",
543 "FNTDEF1", "FNTDEF2", "FNTDEF3", "FNTDEF4", "PRE", "POST", "POSTPOST",
544 "SREFL", "EREFL", NULL, NULL, NULL, NULL
545 };
546
547 static void
548 print_dvi(ubyte ch)
549 {
550 const char *s;
551
552 fprintf(stderr, "%4d %4d ", PXL_H, PXL_V);
553 if (ch <= (ubyte) (SETCHAR0 + 127)) {
554 fprintf(stderr, "SETCHAR%-3d", ch - SETCHAR0);
555 if (isprint(ch))
556 fprintf(stderr, " (%c)", ch);
557 fputc('\n', stderr);
558 return;
559 }
560 else if (ch < FNTNUM0)
561 s = dvi_table1[ch - 128];
562 else if (ch <= (ubyte) (FNTNUM0 + 63)) {
563 fprintf(stderr, "FNTNUM%d\n", ch - FNTNUM0);
564 return;
565 }
566 else
567 s = dvi_table2[ch - (FNTNUM0 + 64)];
568 if (s) {
569 fputs(s, stderr);
570 fputc('\n', stderr);
571 }
572 else
573 XDVI_FATAL((stderr, "print_dvi: unknown op-code %d", ch));
574 }
575
576
577 /*
578 * Count the number of set bits in a 4x4-region of the bitmap
579 */
580
581 static char sample_count[] = {
582 0, 1, 1, 2,
583 1, 2, 2, 3,
584 1, 2, 2, 3,
585 2, 3, 3, 4
586 };
587
588 /*
589 * For greyscaling, return the number of bits that are set in a given region
590 * of width w and height h of the bitmap `bits', starting horizontally after
591 * `bit_skip' bits, where `bytes_wide' is the same as the `bytes_wide' field
592 * in the bitmap struct (scan line width in bytes).
593 * Note that `bits' is really a one-dimensional array, i.e. all the rows
594 * are put in sequence into one single row.
595 */
596 static int
597 sample(bmUnitT *bits, int bytes_wide, int bit_skip, int w, int h)
598 {
599 bmUnitT *beg_ptr, *end_ptr, *curr_ptr;
600 int bits_left;
601 int n, bit_shift, wid;
602
603 #if TRACE_AA
604 fprintf(stderr, "sample: %d bytes wide, %d skip, %d w, %d h\n", bytes_wide, bit_skip, w, h);
605 #endif
606
607 beg_ptr = bits + bit_skip / BMBITS;
608 end_ptr = ADD(bits, h * bytes_wide);
609
610 #if TRACE_AA
611 fprintf(stderr, "beg_ptr: %p, end: %p\n", (void *)beg_ptr, (void *)end_ptr);
612 #endif
613
614 bits_left = w;
615 #ifdef WORDS_BIGENDIAN
616 bit_shift = BMBITS - bit_skip % BMBITS;
617 #else
618 bit_shift = bit_skip % BMBITS;
619 #endif
620 #if TRACE_AA
621 fprintf(stderr, "shift: %d\n", bit_shift);
622 #endif
623 n = 0;
624
625 while (bits_left) {
626 #ifdef WORDS_BIGENDIAN
627 wid = bit_shift;
628 #else
629 wid = BMBITS - bit_shift;
630 #endif
631 if (wid > bits_left)
632 wid = bits_left;
633 if (wid > 4) /* why? */
634 wid = 4;
635 #ifdef WORDS_BIGENDIAN
636 bit_shift -= wid;
637 #endif
638 for (curr_ptr = beg_ptr;
639 curr_ptr < end_ptr;
640 curr_ptr = ADD(curr_ptr, bytes_wide)) {
641 #if TRACE_AA
642 int i;
643 for (i = 0; i < bytes_wide; i++) {
644 fprintf(stderr, "%d ", *(curr_ptr + i));
645 }
646 fprintf(stderr, "\nMask: %d; count: %d\n", wid,
647 sample_count[ (*curr_ptr >> bit_shift) & bit_masks[wid] ]);
648 #endif
649 n += sample_count[ (*curr_ptr >> bit_shift) & bit_masks[wid] ];
650 }
651 #ifdef WORDS_BIGENDIAN
652 if (bit_shift == 0) {
653 bit_shift = BMBITS;
654 ++beg_ptr;
655 }
656 #else
657 bit_shift += wid;
658 if (bit_shift == BMBITS) {
659 bit_shift = 0;
660 ++beg_ptr;
661 }
662 #endif
663 bits_left -= wid;
664 /* fprintf(stderr, "bits_left: %d\n", bits_left); */
665 }
666 return n;
667 }
668
669 static void
670 shrink_glyph(struct glyph *g)
671 {
672 int shrunk_bytes_wide, shrunk_height;
673 int rows_left, rows, init_cols;
674 int cols_left;
675 int cols;
676 bmUnitT *unshrunk_ptr, *shrunk_ptr;
677 bmUnitT m, *cp;
678 /* threshold for which a bit will be set to `on' in the shrunken bitmap */
679 int min_sample = currwin.shrinkfactor * currwin.shrinkfactor * resource.density / 100;
680 int row_num;
681
682 /* These machinations ensure that the character is shrunk according to
683 its hot point, rather than its upper left-hand corner. */
684 g->x2 = g->x / currwin.shrinkfactor;
685 init_cols = g->x - g->x2 * currwin.shrinkfactor;
686 if (init_cols <= 0)
687 init_cols += currwin.shrinkfactor;
688 else
689 ++g->x2;
690 g->bitmap2.w = g->x2 + ROUNDUP((int)g->bitmap.w - g->x, currwin.shrinkfactor);
691 /* include row zero with the positively numbered rows */
692 row_num = g->y + 1;
693 g->y2 = row_num / currwin.shrinkfactor;
694 rows = row_num - g->y2 * currwin.shrinkfactor;
695 if (rows <= 0) {
696 rows += currwin.shrinkfactor;
697 --g->y2;
698 }
699 g->bitmap2.h = shrunk_height
700 = g->y2 + ROUNDUP((int)g->bitmap.h - row_num, currwin.shrinkfactor) + 1;
701 alloc_bitmap(&g->bitmap2);
702
703 unshrunk_ptr = (bmUnitT *) g->bitmap.bits;
704 shrunk_ptr = (bmUnitT *) g->bitmap2.bits;
705 shrunk_bytes_wide = g->bitmap2.bytes_wide;
706 rows_left = g->bitmap.h;
707 memset((char *)shrunk_ptr, '\0', shrunk_bytes_wide * shrunk_height);
708
709 while (rows_left) {
710 if (rows > rows_left)
711 rows = rows_left;
712 cols_left = g->bitmap.w;
713 #ifndef WORDS_BIGENDIAN
714 m = 1; /* XXX was (1 << 0) */
715 #else
716 m = ((bmUnitT)1 << (BMBITS - 1));
717 #endif
718 cp = shrunk_ptr;
719 cols = init_cols;
720 while (cols_left) {
721 /* ??? */
722 if (cols > cols_left)
723 cols = cols_left;
724 /* set a bit to `on' if it's over the threshold */
725 if (sample(unshrunk_ptr, g->bitmap.bytes_wide,
726 (int)g->bitmap.w - cols_left, cols, rows)
727 >= min_sample)
728 *cp |= m;
729 #ifndef WORDS_BIGENDIAN
730 if (m == ((bmUnitT)1 << (BMBITS - 1))) {
731 m = 1; /* XXX was (1 << 0) */
732 ++cp;
733 }
734 else
735 m <<= 1;
736 #else
737 if (m == 1) { /* XXX was (1 << 0) */
738 m = ((bmUnitT)1 << (BMBITS - 1));
739 ++cp;
740 }
741 else
742 m >>= 1;
743 #endif
744 cols_left -= cols;
745 cols = currwin.shrinkfactor;
746 }
747 shrunk_ptr += shrunk_bytes_wide / sizeof(bmUnitT);
748 unshrunk_ptr += rows * g->bitmap.bytes_wide / sizeof(bmUnitT);
749 /* *((char **)&shrunk_ptr) += shrunk_bytes_wide; */
750 /* *((char **)&unshrunk_ptr) += rows * g->bitmap.bytes_wide; */
751 rows_left -= rows;
752 rows = currwin.shrinkfactor;
753 }
754 g->y2 = g->y / currwin.shrinkfactor;
755 if (globals.debug & DBG_BITMAP)
756 print_bitmap(&g->bitmap2);
757 }
758
759 #ifdef GREY
760
761 #ifdef RGB_ANTI_ALIASING
762 /* void */
763 /* filter_colors(Pixel *p1, Pixel *p2) */
764 /* { */
765 /* Pixel res1, res2; */
766
767 /* int res1_r = *p1 & G_visual->red_mask; */
768 /* int res1_g = *p1 & G_visual->green_mask; */
769 /* int res1_b = *p1 & G_visual->blue_mask; */
770
771 /* int res2_r = *p2 & G_visual->red_mask; */
772 /* int res2_g = *p2 & G_visual->green_mask; */
773 /* int res2_b = *p2 & G_visual->blue_mask; */
774 /* } */
775
776 static void
777 color_filter_image(struct glyph *g)
778 {
779 int rows = g->bitmap2.h;
780 int cols = g->bitmap2.w;
781 int i, j, k;
782 int arr_size = rows * cols * 3;
783 unsigned char *arr = xmalloc(arr_size * sizeof *arr);
784 unsigned char *arr2 = xmalloc(arr_size * sizeof *arr2);
785
786 fprintf(stderr, "total: %d\n", arr_size);
787
788 for (i = 0, j = 0; j < rows; j++) {
789 for (k = 0; k < cols; k++) {
790 Pixel p = XGetPixel(g->image2, k, j);
791 #if TRACE_AA3
792 fprintf(stderr, "k: %d, j: %d, idx: %d\n", k, j, i);
793 #endif
794 arr[i] = (p & G_visual->red_mask) >> 16;
795 arr[i + 1] = (p & G_visual->green_mask) >> 8;
796 arr[i + 2] = (p & G_visual->blue_mask);
797 #if TRACE_AA3
798 fprintf(stderr, "0x%.6lX -> %d, %d, %d\n", p, arr[i], arr[i + 1], arr[i + 2]);
799 #endif
800 i += 3;
801 }
802 }
803
804 for (i = 0; i < arr_size; i++) {
805 if (i == 0) { /* merge two elems */
806 if (i == arr_size - 1) /* only one elem, do nothing */
807 break;
808 arr2[i] = (int)(resource.subpixel_energy[0] * arr[i] +
809 resource.subpixel_energy[1] * arr[i + 1] + 0.5);
810 }
811 else if (i == arr_size - 1) { /* merge two elems */
812 if (i == 0) /* only one elem, do nothing */
813 break;
814 arr2[i] = (int)(resource.subpixel_energy[1] * arr[i - 1] +
815 resource.subpixel_energy[0] * arr[i] + 0.5);
816 }
817 else { /* merge three elems */
818 arr2[i] = (int)(resource.subpixel_energy[1] * arr[i - 1] +
819 resource.subpixel_energy[0] * arr[i] +
820 resource.subpixel_energy[1] * arr[i + 1] + 0.5);
821 #if TRACE_AA3
822 fprintf(stderr, "%d, %d, %d -> %d\n", arr[i - 1], arr[i], arr[i + 1], arr2[i]);
823 #endif
824 }
825 }
826
827 for (i = 0, j = 0; j < rows; j++) {
828 for (k = 0; k < cols; k++) {
829 Pixel p = arr2[i] << 16 | (arr2[i + 1] << 8) | arr2[i + 2];
830 #if TRACE_AA3
831 fprintf(stderr, "%d, %d, %d -> 0x%.6lX\n", arr2[i], arr2[i + 1], arr2[i + 2], p);
832 #endif
833 XPutPixel(g->image2, k, j, p);
834 i += 3;
835 }
836 }
837
838 free(arr);
839 free(arr2);
840 }
841
842 #endif
843
844 static void
845 shrink_glyph_grey(struct glyph *g)
846 {
847 int rows_left, rows, init_cols;
848 int cols_left;
849 int cols;
850 int x, y;
851 long thesample;
852 /* int min_sample = currwin.shrinkfactor * currwin.shrinkfactor * resource.density / 100; */
853 Pixel onoff, onoff2;
854 bmUnitT *unshrunk_ptr;
855 unsigned int size;
856 int row_num;
857
858 #if COLOR
859 if (fg_active != fg_current) {
860 do_color_change();
861 }
862 #endif
863 if (pixeltbl == NULL) { /* fix #1611508 (segfault when starting with -nogrey) */
864 do_color_change();
865 }
866
867 /* TODO: rounding errors causing color fringing (see HACK comment below):
868
869 \documentclass{article}
870 \pagestyle{empty}
871
872 \begin{document}
873 l
874 \end{document}
875
876 With ./xdvi-xaw.bin -name xdvi -subpixel rgb -s 4 ./test.dvi:
877
878 subpixel order: rgb = 1
879 g->x2: 0, init_cols: -2
880 AFTER: g->x2: 0, init_cols: 2
881
882 but with ./xdvi-xaw.bin -name xdvi -s 4 ./test.dvi:
883
884 g->x2: 0, init_cols: -3
885 AFTER: g->x2: 0, init_cols: 1
886 */
887
888 /* These machinations ensure that the character is shrunk according to
889 its hot point, rather than its upper left-hand corner. */
890 #ifdef RGB_ANTI_ALIASING
891 if (resource.subpixel_order == SUBPIXEL_NONE)
892 g->x2 = g->x / currwin.shrinkfactor;
893 else {
894 if (g->x < 0)
895 g->x2 = (int)(g->x / 3.0 - 0.5) / currwin.shrinkfactor;
896 else
897 g->x2 = (int)(g->x / 3.0 + 0.5) / currwin.shrinkfactor;
898 }
899
900 if (resource.subpixel_order == SUBPIXEL_NONE)
901 init_cols = g->x - g->x2 * currwin.shrinkfactor;
902 else {
903 if (g->x < 0)
904 init_cols = (int)(g->x / 3.0 - 0.5) - g->x2 * currwin.shrinkfactor;
905 else
906 init_cols = (int)(g->x / 3.0 + 0.5) - g->x2 * currwin.shrinkfactor;
907 fprintf(stderr, "g->x: %d, g->x2: %d, init_cols: %d\n", g->x, g->x2, init_cols);
908 }
909 #else
910 g->x2 = g->x / currwin.shrinkfactor;
911 init_cols = g->x - g->x2 * currwin.shrinkfactor;
912 #endif
913
914 if (init_cols <= 0)
915 init_cols += currwin.shrinkfactor;
916 else
917 ++(g->x2);
918
919 #ifdef RGB_ANTI_ALIASING
920
921 if (resource.subpixel_order == SUBPIXEL_NONE)
922 g->bitmap2.w = g->x2 + ROUNDUP((int)g->bitmap.w - g->x, currwin.shrinkfactor);
923 else {
924 fprintf(stderr, "AFTER: g->x2: %d, init_cols: %d\n", g->x2, init_cols);
925 /* fprintf(stderr, "g->bitmap.w / 3.0 + 0.5: %d; g->x / 3.0: %d; all: %d, %d\n", */
926 /* (int)(g->bitmap.w / 3.0 + 0.5), */
927 /* (int)(g->x / 3.0), */
928 /* (int)((g->bitmap.w / 3.0 + 0.5) - g->x / 3.0), */
929 /* g->x2 + (int)((((g->bitmap.w / 3.0 + 0.5) - g->x / 3.0) + currwin.shrinkfactor - 1) / currwin.shrinkfactor)); */
930 g->bitmap2.w = g->x2 + ROUNDUP((int)((g->bitmap.w / 3.0 + 0.5) - g->x / 3.0), currwin.shrinkfactor);
931 /* fprintf(stderr, "g->bitmap.w: %d\n", g->bitmap2.w); */
932 }
933 #else
934 g->bitmap2.w = g->x2 + ROUNDUP((int)g->bitmap.w - g->x, currwin.shrinkfactor);
935 #endif
936
937 /* include row zero with the positively numbered rows */
938 row_num = g->y + 1;
939 #ifdef DBG_AA
940 fprintf(stderr, "row_num: %d\n", row_num);
941 #endif /* DBG_AA */
942 /* g->y2 is the new height of the image: */
943 g->y2 = row_num / currwin.shrinkfactor;
944 #ifdef DBG_AA
945 fprintf(stderr, "g->y2: %d\n", g->y2);
946 #endif /* DBG_AA */
947 /* in C89 and before, result of division can `truncate towards negative infinity'
948 (i.e., round to the larger digit) for negative quotients, hence we need
949 to test for rows < 0. OTOH, if rows = 0, use currwin.shrinkfactor instead
950 (why?)
951 */
952 if ((rows = row_num - g->y2 * currwin.shrinkfactor) <= 0) {
953 rows += currwin.shrinkfactor;
954 --(g->y2);
955 }
956 g->bitmap2.h = g->y2 + ROUNDUP((int)g->bitmap.h - row_num, currwin.shrinkfactor) + 1;
957
958 #if TRACE_AA1
959 if (resource.subpixel_order != SUBPIXEL_NONE) {
960 fprintf(stderr, "\nbitmap.h: %d, bitmap.w: %d\n", g->bitmap2.h, g->bitmap2.w);
961 }
962 #endif /* DBG_AA */
963
964 /* allocate pixmap for antialiasing ... */
965 if (g->pixmap2 == NULL) {
966 g->image2 = XCreateImage(DISP, G_visual, G_depth, ZPixmap,
967 0, (char *)NULL, g->bitmap2.w, g->bitmap2.h,
968 BMBITS, 0);
969 size = g->image2->bytes_per_line * g->bitmap2.h;
970 g->pixmap2 = g->image2->data = xmalloc(size != 0 ? size : 1);
971 }
972 /* ... and the pixmap used for globals.gc.fore2: */
973 if (globals.gc.fore2 != NULL && g->pixmap2_gc2 == NULL) {
974 size = g->image2->bytes_per_line * g->bitmap2.h;
975 g->pixmap2_gc2 = xmalloc(size != 0 ? size : 1);
976 }
977
978 #if 0
979 if (resource.subpixel_order != SUBPIXEL_NONE) {
980 fprintf(stderr, "\n============= BITMAP ==============\n");
981 print_bitmap(&g->bitmap);
982 }
983 #endif
984
985 unshrunk_ptr = (bmUnitT *)g->bitmap.bits;
986 rows_left = g->bitmap.h;
987 y = 0;
988 /* the basic algorithm is the same as in the nogrey code, with the main
989 exception that the return value of sample() is used. */
990 while (rows_left) {
991 #ifdef RGB_ANTI_ALIASING
992 Pixel pixel = 0;
993 Pixel pixel2 = 0;
994 #endif
995 x = 0;
996 if (rows > rows_left) /* why - extra safety? */
997 rows = rows_left;
998 cols_left = g->bitmap.w;
999 /* fprintf(stderr, "init_cols: %d\n", init_cols); */
1000 cols = init_cols;
1001 while (cols_left) {
1002 if (cols > cols_left) /* why - extra safety? */
1003 cols = cols_left;
1004
1005 thesample = sample(unshrunk_ptr, g->bitmap.bytes_wide,
1006 (int)g->bitmap.w - cols_left, cols, rows);
1007
1008 /* if (resource.subpixel_order != SUBPIXEL_NONE && resource.subpixel_energy[2] != 0) */
1009 /* onoff = thesample >= min_sample ? 0xffffff : 0; */
1010 /* else */
1011 onoff = pixeltbl[thesample];
1012
1013 #ifdef XSERVER_INFO
1014 if (globals.debug & DBG_PK) {
1015 int c;
1016 /* fprintf(stderr, "onoff: %d\n", onoff); */
1017 if (onoff > 65536)
1018 c = onoff / 65536;
1019 else if (onoff > 256)
1020 c = onoff / 256;
1021 else
1022 c = onoff;
1023 if (c == 0)
1024 fprintf(stdout, ",..");
1025 else
1026 fprintf(stdout, ",%.2x", c);
1027 }
1028 #endif
1029
1030 #ifdef RGB_ANTI_ALIASING
1031 if (resource.subpixel_order != SUBPIXEL_NONE) {
1032 int div = x / 3;
1033 int rest = x % 3;
1034
1035 if (resource.subpixel_order == SUBPIXEL_RGB) {
1036 if (rest == 0)
1037 pixel = onoff & G_visual->red_mask;
1038 else if (rest == 1)
1039 pixel |= onoff & G_visual->green_mask;
1040 else
1041 pixel |= onoff & G_visual->blue_mask;
1042 }
1043 else { /* SUBPIXEL_BGR */
1044 if (rest == 0)
1045 pixel = onoff & G_visual->blue_mask;
1046 else if (rest == 1)
1047 pixel |= onoff & G_visual->green_mask;
1048 else
1049 pixel |= onoff & G_visual->red_mask;
1050 }
1051
1052 #if 0 && TRACE_AA1
1053 fprintf(stderr, "sample: %ld; row %d, col %d, left %d: 0x%.6lx; pixel: 0x%.6lx at pos %d,%d (x:%d)\n",
1054 thesample, rows, cols, cols_left, onoff, pixel, div, y, x);
1055 #endif /* TRACE_AA */
1056 fprintf(stderr, "pixel at %d\n", div);
1057 XPutPixel(g->image2, div, y, pixel);
1058
1059 /* HACK to fix color fringing problem */
1060 if (div + 1 < g->bitmap2.w) {
1061 fprintf(stderr, "rest pixel at %d\n", div);
1062 XPutPixel(g->image2, div + 1, y, 0);
1063 }
1064 }
1065 else {
1066 #if 0 && TRACE_AA1
1067 fprintf(stderr, "sample: %ld; row %d, col %d, left %d: 0x%.6lx at pos %d, %d\n",
1068 thesample, rows, cols, cols_left, onoff, x, y);
1069 #endif /* TRACE_AA */
1070 XPutPixel(g->image2, x, y, onoff);
1071 }
1072 #else
1073 XPutPixel(g->image2, x, y, onoff);
1074 #endif /* RGB_ANTI_ALIASING */
1075 if (globals.gc.fore2 != NULL) {
1076 onoff2 = pixeltbl_gc2[thesample];
1077 #ifdef RGB_ANTI_ALIASING
1078 if (resource.subpixel_order != SUBPIXEL_NONE) {
1079 int div = x / 3;
1080 int rest = x % 3;
1081
1082 if (resource.subpixel_order == SUBPIXEL_RGB) {
1083 if (rest == 0)
1084 pixel2 = onoff2 & G_visual->red_mask;
1085 else if (rest == 1)
1086 pixel2 |= onoff2 & G_visual->green_mask;
1087 else
1088 pixel2 |= onoff2 & G_visual->blue_mask;
1089 }
1090 else { /* SUBPIXEL_BGR */
1091 if (rest == 0)
1092 pixel2 = onoff2 & G_visual->blue_mask;
1093 else if (rest == 1)
1094 pixel2 |= onoff2 & G_visual->green_mask;
1095 else
1096 pixel2 |= onoff2 & G_visual->red_mask;
1097 }
1098
1099 g->image2->data = g->pixmap2_gc2;
1100 #if TRACE_AA1
1101 fprintf(stderr, "fore2 at %d, %d\n", div, y);
1102 #endif
1103 XPutPixel(g->image2, div, y, pixel2);
1104 g->image2->data = g->pixmap2;
1105 }
1106 else {
1107 #else /* RGB_ANTI_ALIASING */
1108 g->image2->data = g->pixmap2_gc2;
1109 XPutPixel(g->image2, x, y, onoff2);
1110 g->image2->data = g->pixmap2;
1111 #endif
1112 #ifdef RGB_ANTI_ALIASING
1113 }
1114 #endif
1115 }
1116
1117 #if 0 && TRACE_AA1
1118 fprintf(stderr, "subtracting %d from %d: %d\n", cols, cols_left, cols_left - cols);
1119 #endif
1120 cols_left -= cols;
1121 cols = currwin.shrinkfactor;
1122 x++;
1123 }
1124 /* advance pointer by the number of rows covered */
1125 /* fprintf(stderr, "++: %d; %d, %d\n", rows * g->bitmap.bytes_wide, g->bitmap.bytes_wide, sizeof(bmUnitT)); */
1126 unshrunk_ptr += rows * g->bitmap.bytes_wide / sizeof(bmUnitT);
1127 /* *((char **)&unshrunk_ptr) += rows * g->bitmap.bytes_wide; */
1128 rows_left -= rows;
1129 rows = currwin.shrinkfactor;
1130 y++;
1131 #ifdef XSERVER_INFO
1132 if (globals.debug & DBG_PK)
1133 fprintf(stdout, "\n");
1134 #endif
1135 }
1136 #ifdef XSERVER_INFO
1137 if (globals.debug & DBG_PK)
1138 fprintf(stdout, "\n");
1139 #endif
1140
1141 #if 0
1142 fprintf(stderr, "y: %d, bitmap.h: %d\n", y, (int)g->bitmap2.h);
1143 #endif /* DBG_AA */
1144
1145 /* fill remaining rows not covered before (how?) */
1146 while (y < (int)g->bitmap2.h) {
1147 for (x = 0; x < (int)g->bitmap2.w; x++) {
1148 /* int c = *pixeltbl; */
1149 #if TRACE_AA1
1150 fprintf(stderr, "Remaining at %d, %d: 0x%.6lx\n", x, y, *pixeltbl);
1151 #endif
1152 XPutPixel(g->image2, x, y, *pixeltbl);
1153 /* if (c == 0) */
1154 /* fprintf(stdout, ",.."); */
1155 /* else */
1156 /* fprintf(stdout, ",%.2x", c); */
1157 if (globals.gc.fore2 != NULL) {
1158 g->image2->data = g->pixmap2_gc2;
1159 #if TRACE_AA1
1160 fprintf(stderr, "image2 at %d, %d: 0x%.6lx\n", x, y, *pixeltbl_gc2);
1161 #endif
1162 XPutPixel(g->image2, x, y, *pixeltbl_gc2);
1163 g->image2->data = g->pixmap2;
1164 }
1165 }
1166 y++;
1167 }
1168
1169 #ifdef RGB_ANTI_ALIASING
1170 if (resource.subpixel_order != SUBPIXEL_NONE && resource.subpixel_energy[1] != 0)
1171 color_filter_image(g);
1172 if (resource.subpixel_order != SUBPIXEL_NONE && resource.subpixel_energy[2] != 0)
1173 color_filter_image(g);
1174 #endif
1175
1176 g->y2 = g->y / currwin.shrinkfactor;
1177 #if COLOR
1178 g->fg = fg_current;
1179 #endif
1180 }
1181 #endif /* GREY */
1182
1183 /*
1184 * Find font #n.
1185 */
1186
1187 static void
1188 change_font(unsigned long n)
1189 {
1190 struct tn *tnp;
1191
1192 if (n < currinf.tn_table_len)
1193 currinf.fontp = currinf.tn_table[n];
1194 else {
1195 currinf.fontp = NULL;
1196 for (tnp = currinf.tn_head; tnp != NULL; tnp = tnp->next)
1197 if (tnp->TeXnumber == n) {
1198 currinf.fontp = tnp->fontp;
1199 break;
1200 }
1201 }
1202 if (currinf.fontp == NULL)
1203 XDVI_FATAL((stderr, "Non-existent font number %ld", n));
1204 if (currinf.fontp->set_char_p == NULL)
1205 XDVI_FATAL((stderr, "No procedure to set font %ld, %s", n, currinf.fontp->fontname));
1206 maxchar = currinf.fontp->maxchar;
1207 currinf.set_char_p = currinf.fontp->set_char_p;
1208 }
1209
1210
1211 /*
1212 * Open a font file.
1213 */
1214
1215 void
1216 open_font_file(struct font *fontp)
1217 {
1218 if (fontp->file == NULL) {
1219 fontp->file = XFOPEN(fontp->filename, OPEN_MODE);
1220 if (fontp->file == NULL)
1221 XDVI_FATAL((stderr, "Couldn't re-locate font file `%s'", fontp->filename));
1222 }
1223 }
1224
1225 /*
1226 * Read and return a 0-terminated special string allocated in static memory
1227 * (i.e. contents will be overwritten by next call of this function).
1228 */
1229
1230 static char *
1231 read_special(FILE *fp, long nbytes)
1232 {
1233 static char *spcl = NULL;
1234 static long spcl_len = -1;
1235 char *p;
1236
1237 if (nbytes > spcl_len) {
1238 spcl = xrealloc(spcl, (unsigned)nbytes + 1);
1239 spcl_len = nbytes;
1240 }
1241 p = spcl;
1242 for (;;) {
1243 int i = currinf.end - currinf.pos;
1244
1245 if (i > nbytes)
1246 i = nbytes;
1247 memcpy(p, (char *)currinf.pos, i);
1248 currinf.pos += i;
1249 p += i;
1250 nbytes -= i;
1251 if (nbytes == 0)
1252 break;
1253 (void)xxone(fp);
1254 --currinf.pos;
1255 }
1256 *p = '\0';
1257 return spcl;
1258 }
1259
1260 /*
1261 * Table used for scanning. If >= 0, then skip that many bytes.
1262 * M1 means end of page, M2 means special, M3 means FNTDEF,
1263 * M4 means unrecognizable, and M5 means doesn't belong here.
1264 */
1265
1266 #define M1 255
1267 #define M2 254
1268 #define M3 253
1269 #define M4 252
1270 #define M5 251
1271 #define MM 251
1272
1273 static ubyte scantable[256] = {
1274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* chars 0 - 127 */
1275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1282 1, 2, /* SET1,SET2 (128,129) */
1283 /* -,-,SETRULE,PUT1,PUT2,-,-,PUTRULE,NOP,BOP (130-139) */
1284 M4, M4, 8, 1, 2, M4, M4, 8, 0, 44,
1285 M1, 0, 0, 1, 2, 3, 4, 0, 1, 2, /* EOP,PUSH,POP,RIGHT1-4,W0M2 (140-149) */
1286 3, 4, 0, 1, 2, 3, 4, 1, 2, 3, /* W3-4,X0-4,DOWN1-3 (150-159) */
1287 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, /* DOWN4,Y0-4,Z0-3 (160-169) */
1288 4, /* Z4 (170) */
1289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* change font 171 - 234 */
1290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1293 1, 2, 3, 4, M2, /* FNT1-4,XXX1 (235-239) */
1294 /* XXX2-4,FNTDEF1-4,PRE,POST,POSTPOST (240-249) */
1295 M2, M2, M2, M3, M3, M3, M3, M5, M5, M5,
1296 0, 0, M4, M4, M4, M4
1297 }; /* SREFL,EREFL,-,-,-,- (250-255) */
1298
1299 /*
1300 * This is the generic scanning routine. It assumes that currinf, etc.
1301 * are ready to go at the start of the page to be scanned.
1302 */
1303
1304
1305
1306 Boolean
1307 spcl_scan(Boolean (*spcl_proc)(char *str, int str_len, void *data), void *data, Boolean return_if_found, FILE *fp)
1308 {
1309 ubyte ch;
1310 ubyte n;
1311 long a;
1312
1313 for (;;) {
1314 ch = xone(fp);
1315 /* print_dvi(ch); */
1316 n = scantable[ch];
1317 if (n < MM)
1318 while (n-- != 0)
1319 (void)xone(fp);
1320 else if (n == M1)
1321 break; /* end of page */
1322 else
1323 switch (n) {
1324 case M2: /* special */
1325 a = xnum(fp, ch - XXX1 + 1);
1326 if (a > 0) {
1327 if (spcl_proc(read_special(fp, a), a, data) && return_if_found) {
1328 return True;
1329 }
1330 }
1331 break;
1332 case M3: /* FNTDEF */
1333 xskip(fp, (long)(12 + ch - FNTDEF1 + 1));
1334 ch = xone(fp);
1335 xskip(fp, (long)ch + (long)xone(fp));
1336 break;
1337 case M4: /* unrecognizable */
1338 XDVI_FATAL((stderr, "unknown op-code %d", ch));
1339 break;
1340 case M5: /* doesn't belong */
1341 dvi_fmt_error("spcl_scan: shouldn't happen: %s encountered",
1342 dvi_table2[ch - (FNTNUM0 + 64)]);
1343 break;
1344 }
1345 }
1346 return False;
1347 }
1348
1349 #define xspell_conv(n) spell_conv0(n, current_dimconv)
1350
1351 /*
1352 * Prescanning routine for dvi file. This looks for specials like
1353 * `header=' and `!'.
1354 */
1355
1356 void
1357 prescan(FILE *fp)
1358 {
1359 if (fp == NULL) {
1360 return;
1361 }
1362
1363 TRACE_FILES((stderr, "prescan on %p", (void *)fp));
1364
1365 (void)lseek(fileno(fp), pageinfo_get_offset(scanned_page + 1), SEEK_SET);
1366 G_dvi_buf_ptr = dvi_buffer;
1367 currinf.pos = currinf.end = G_dvi_buf_ptr;
1368 for (;;) {
1369 if (scanned_page == -1) { /* on first page */
1370 TRACE_FILES((stderr, "prescan on page 1"));
1371 pageinfo_set_page_width(scanned_page + 1, pageinfo_get_page_width(total_pages));
1372 pageinfo_set_page_height(scanned_page + 1, pageinfo_get_page_height(total_pages));
1373 pageinfo_set_window_width(scanned_page + 1, pageinfo_get_window_width(total_pages));
1374 pageinfo_set_window_height(scanned_page + 1, pageinfo_get_window_height(total_pages));
1375 }
1376 else {
1377 TRACE_FILES((stderr, "prescan on page %d", scanned_page));
1378 pageinfo_set_page_width(scanned_page + 1, pageinfo_get_page_width(scanned_page));
1379 pageinfo_set_page_height(scanned_page + 1, pageinfo_get_page_height(scanned_page));
1380 pageinfo_set_window_width(scanned_page + 1, pageinfo_get_window_width(scanned_page));
1381 pageinfo_set_window_height(scanned_page + 1, pageinfo_get_window_height(scanned_page));
1382 }
1383 if (read_events(EV_NOWAIT) & EV_GE_NEWPAGE) {
1384 break;
1385 }
1386 /* NOTE: longjmp(globals.ev.canit) should not be done within
1387 read_events(). */
1388 htex_prescan_save();
1389 htex_prescan_initpage();
1390
1391 if (!setjmp(globals.ev.canit)) {
1392 struct htex_prescan_data data;
1393 int pre_depth, depth;
1394 data.pageno = scanned_page + 1;
1395 data.scan_type = HTEX_ANCHOR_NUM; /* just for the anchor numbers */
1396 pre_depth = htex_prescan_get_depth();
1397 (void)spcl_scan(scan_special, &data, False, fp);
1398
1399 depth = htex_prescan_get_depth();
1400
1401 if (depth > pre_depth) {
1402 /* we have a mismatched anchor. We currently don't deal with
1403 _nested_ mismatched anchors (ugh), so there's only one
1404 anchor string that can be used as info on the next page. */
1405 int anchor_num = htex_prescan_get_mismatched_anchor_num(depth);
1406 /* scan again to find the anchor string at anchor number `anchor_num' */
1407 (void)lseek(fileno(fp), pageinfo_get_offset(scanned_page + 1), SEEK_SET);
1408 currinf.pos = currinf.end = G_dvi_buf_ptr;
1409 data.anchor_num = anchor_num;
1410 data.scan_type = HTEX_ANCHOR_STRING;
1411 htex_prescan_reset_firstpass();
1412 (void)spcl_scan(scan_special, &data, False, fp);
1413 depth = htex_prescan_get_depth();
1414 }
1415 else if (depth > 0 && scanned_page >= 0) { /* mismatch was on a previous page */
1416 htex_prescan_carry_over(scanned_page, scanned_page + 1);
1417 }
1418 }
1419 else { /* if interrupted */
1420 htex_prescan_restore(scanned_page + 1);
1421 #if PS
1422 psp.interrupt();
1423 #endif
1424 break;
1425 }
1426 if (globals.ev.flags & EV_GE_NEWPAGE)
1427 break;
1428 ++scanned_page;
1429 #if COLOR
1430 if (scanned_page_color < scanned_page) {
1431 scan_color_eop();
1432 scanned_page_color = scanned_page;
1433 }
1434 #endif
1435 #if PS
1436 if (scanned_page_ps < scanned_page)
1437 scanned_page_ps = scanned_page;
1438 #endif
1439 if (scanned_page >= current_page)
1440 break;
1441 }
1442
1443 #if PS
1444 if (!(globals.ev.flags & EV_GE_NEWPAGE))
1445 psp.endheader();
1446 #endif
1447 }
1448
1449 /*
1450 * Routines to print characters.
1451 */
1452
1453 setcharRetvalT
1454 set_char(
1455 #ifdef TEXXET
1456 wide_ubyte cmd,
1457 #endif
1458 wide_ubyte ch)
1459 {
1460 struct glyph *g;
1461 #ifdef TEXXET
1462 long dvi_h_sav;
1463 #endif
1464
1465 if (ch > maxchar)
1466 realloc_font(currinf.fontp, (wide_ubyte)ch);
1467 if ((g = &currinf.fontp->glyph[ch])->bitmap.bits == NULL) {
1468 if (g->addr == 0) {
1469 if (!resource.hush_chars)
1470 XDVI_WARNING((stderr, "Character %d not defined in font %s", ch, currinf.fontp->fontname));
1471 g->addr = -1;
1472 #ifdef TEXXET
1473 return;
1474 #else
1475 return 0L;
1476 #endif
1477 }
1478 if (g->addr == -1) {
1479 #ifdef TEXXET
1480 return;
1481 #else
1482 return 0L; /* previously flagged missing char */
1483 #endif
1484 }
1485 #if FREETYPE
1486 if (currinf.fontp->ft == NULL) /* if not freetype font */
1487 #endif
1488 {
1489 open_font_file(currinf.fontp);
1490 fseek(currinf.fontp->file, g->addr, SEEK_SET);
1491 }
1492 (*currinf.fontp->read_char) (currinf.fontp, ch);
1493 if (globals.debug & DBG_BITMAP)
1494 print_char((ubyte) ch, g);
1495 currinf.fontp->timestamp = ++current_timestamp;
1496 }
1497
1498 #ifdef TEXXET
1499 dvi_h_sav = DVI_H;
1500 if (currinf.dir < 0)
1501 DVI_H -= g->dvi_adv;
1502
1503 if (scan_frame == NULL) {
1504 #endif
1505
1506 #ifdef RGB_ANTI_ALIASING
1507 if (currwin.shrinkfactor == -1) {
1508 put_bitmap(&g->bitmap, PXL_H - g->x, PXL_V - g->y);
1509 }
1510 #ifdef __GNUC__
1511 #warning TODO: implement horizontal AA at shrink 1
1512 #endif
1513 #else
1514 if (currwin.shrinkfactor == 1) {
1515 put_bitmap(&g->bitmap, PXL_H - g->x, PXL_V - g->y);
1516 }
1517 #endif
1518 else {
1519 #ifdef GREY
1520 if (resource.use_grey) {
1521 if (g->pixmap2 == NULL) {
1522 #ifdef DBG_AA
1523 fprintf(stderr, "shrinking the bitmap!\n");
1524 #endif /* DBG_AA */
1525 /* print_bitmap(&g->bitmap); */
1526 shrink_glyph_grey(g);
1527 }
1528 put_image(g, PXL_H - g->x2, PXL_V - g->y2);
1529 }
1530 else {
1531 if (g->bitmap2.bits == NULL) {
1532 shrink_glyph(g);
1533 }
1534 put_bitmap(&g->bitmap2, PXL_H - g->x2, PXL_V - g->y2);
1535 }
1536 #else
1537 if (g->bitmap2.bits == NULL) {
1538 shrink_glyph(g);
1539 }
1540 put_bitmap(&g->bitmap2, PXL_H - g->x2, PXL_V - g->y2);
1541 #endif
1542 }
1543 #ifdef TEXXET
1544 }
1545 if (cmd == PUT1 || (resource.omega && cmd == PUT2))
1546 DVI_H = dvi_h_sav;
1547 else if (currinf.dir > 0)
1548 DVI_H += g->dvi_adv;
1549 return;
1550 #else
1551 return g->dvi_adv;
1552 #endif
1553 }
1554
1555 /*
1556 * Routines to print characters.
1557 */
1558
1559 static void
1560 warn_setting_empty_char(void)
1561 {
1562 /* this is probably serious enough for a GUI warning */
1563 popup_message(globals.widgets.top_level,
1564 MSG_ERR,
1565 /* helptext */
1566 "Xdvi tries all of the following possibilities in turn, and "
1567 "all of them have failed:\n\n"
1568 " (1) If the resource type1 is set, try a PostScript Type 1 "
1569 "version of a font.\n\n"
1570 " (2) Otherwise, or if the Type 1 version hasn't been found, "
1571 "try to "
1572 "locate, or generate via mktexpk, a TeX Pixel (PK) version of "
1573 "the font.\n\n"
1574 " (3) Use the fallback font defined via the \"altfont\" "
1575 "resource (cmr10 by default), "
1576 "both as Type 1 and as PK version, at various resolutions.\n\n"
1577 "It seems that your font setup is defective.\n",
1578 /* errmsg */
1579 "Error loading font %s: Neither a Type 1 version nor "
1580 "a pixel version could be found. The character(s) "
1581 "will be left blank.",
1582 currinf.fontp->fontname);
1583 }
1584
1585 static setcharRetvalT
1586 set_empty_char(
1587 #ifdef TEXXET
1588 wide_ubyte cmd,
1589 #endif
1590 wide_ubyte ch)
1591 {
1592 #ifdef TEXXET
1593 UNUSED(cmd);
1594 #endif
1595 UNUSED(ch);
1596 #ifdef TEXXET
1597 return;
1598 #else
1599 return 0L;
1600 #endif
1601 }
1602
1603 setcharRetvalT
1604 load_n_set_char(
1605 #ifdef TEXXET
1606 wide_ubyte cmd,
1607 #endif
1608 wide_ubyte ch)
1609 {
1610 if (!load_font(currinf.fontp
1611 #if DELAYED_MKTEXPK
1612 , True
1613 #endif
1614 )) { /* if not found */
1615 if (globals.ev.flags & EV_GE_NEWDOC) { /* if abort */
1616 longjmp(globals.ev.canit, 1);
1617 }
1618
1619 currinf.set_char_p = currinf.fontp->set_char_p = set_empty_char;
1620 warn_setting_empty_char();
1621
1622 #ifdef TEXXET
1623 return;
1624 #else
1625 return 0L;
1626 #endif
1627 }
1628 maxchar = currinf.fontp->maxchar;
1629 currinf.set_char_p = currinf.fontp->set_char_p;
1630 #ifdef TEXXET
1631 (*currinf.set_char_p) (cmd, ch);
1632 return;
1633 #else
1634 return (*currinf.set_char_p) (ch);
1635 #endif
1636 }
1637
1638 setcharRetvalT
1639 set_vf_char(
1640 #ifdef TEXXET
1641 wide_ubyte cmd,
1642 #endif
1643 wide_ubyte ch)
1644 {
1645 struct macro *m;
1646 struct drawinf oldinfo;
1647 wide_ubyte oldmaxchar;
1648 static ubyte c;
1649 #ifdef TEXXET
1650 long dvi_h_sav;
1651 #endif
1652
1653 if (ch > maxchar)
1654 realloc_virtual_font(currinf.fontp, ch);
1655 if ((m = &currinf.fontp->macro[ch])->pos == NULL) {
1656 if (!resource.hush_chars)
1657 XDVI_WARNING((stderr, "Character %d not defined in font %s", ch, currinf.fontp->fontname));
1658 m->pos = m->end = &c;
1659 #ifdef TEXXET
1660 return;
1661 #else
1662 return 0L;
1663 #endif
1664 }
1665 #ifdef TEXXET
1666 dvi_h_sav = DVI_H;
1667 if (currinf.dir < 0)
1668 DVI_H -= m->dvi_adv;
1669 if (scan_frame == NULL) {
1670 #endif
1671 oldinfo = currinf;
1672 if (!currinf.virtual)
1673 dvi_pointer_frame = &oldinfo;
1674 oldmaxchar = maxchar;
1675 WW = XX = YY = ZZ = 0;
1676 currinf.tn_table_len = VFTABLELEN;
1677 currinf.tn_table = currinf.fontp->vf_table;
1678 currinf.tn_head = currinf.fontp->vf_chain;
1679 currinf.pos = m->pos;
1680 currinf.end = m->end;
1681 currinf.virtual = currinf.fontp;
1682
1683 draw_part(globals.dvi_file.bak_fp, current_frame, currinf.fontp->dimconv);
1684 if (currinf.pos != currinf.end + 1)
1685 dvi_fmt_error("virtual character macro does not end correctly");
1686 currinf = oldinfo;
1687 if (!currinf.virtual)
1688 dvi_pointer_frame = &currinf;
1689 maxchar = oldmaxchar;
1690 #ifdef TEXXET
1691 }
1692 if (cmd == PUT1 || (resource.omega && cmd == PUT2))
1693 DVI_H = dvi_h_sav;
1694 else if (currinf.dir > 0)
1695 DVI_H += m->dvi_adv;
1696 return;
1697 #else
1698 return m->dvi_adv;
1699 #endif
1700 }
1701
1702
1703 #if FREETYPE
1704
1705 /*
1706 * set_ft_char() is used as a set_char routine to handle delayed loading
1707 * of freetype fonts. See more details in ft.c.
1708 */
1709
1710 static void
1711 do_load_freetype_font(void)
1712 {
1713 if (!load_ft_font(currinf.fontp)) {
1714 /* Revert to non-scalable font */
1715 struct ftfont *ftp;
1716
1717 TRACE_FT((stderr,
1718 "Font %s is not loadable; reverting to non-scalable font",
1719 currinf.fontp->fontname));
1720
1721 ftp = currinf.fontp->ft;
1722 ftp->t1->bad = True;
1723 if (currinf.fontp == ftp->first_size) {
1724 if (currinf.fontp->next_size == NULL) {
1725 /* if this is the only size of this font face */
1726 ftp->t1->ft = NULL;
1727 free(ftp);
1728 }
1729 else {
1730 struct font *fontp2;
1731
1732 ftp->first_size = fontp2 = currinf.fontp->next_size;
1733 /*
1734 * Opening the file might have succeeded at some other size,
1735 * so we need to transfer that information to the next
1736 * record in case it was put here.
1737 */
1738 fontp2->file = currinf.fontp->file;
1739 currinf.fontp->file = NULL;
1740 fontp2->filename = currinf.fontp->filename;
1741 currinf.fontp->filename = NULL;
1742 fontp2->timestamp = currinf.fontp->timestamp;
1743 }
1744 }
1745 else {
1746 struct font *fontp2;
1747
1748 fontp2 = ftp->first_size;
1749 while (fontp2->next_size != currinf.fontp)
1750 fontp2 = fontp2->next_size;
1751 fontp2->next_size = currinf.fontp->next_size;
1752 }
1753 currinf.fontp->ft = NULL;
1754 /* The virtual font machinery will take it from here. */
1755 /* That will call load_font(), but it won't take us back to the */
1756 /* freetype font, because that font will have been marked bad. */
1757 currinf.set_char_p = load_n_set_char;
1758 }
1759 else
1760 currinf.set_char_p = currinf.fontp->set_char_p = set_char;
1761 }
1762
1763 setcharRetvalT
1764 set_ft_char(
1765 # if TEXXET
1766 wide_ubyte cmd,
1767 # endif
1768 wide_ubyte ch)
1769 {
1770 do_load_freetype_font();
1771
1772 # if !TEXXET
1773 return (*currinf.set_char_p)(ch);
1774 # else
1775 (*currinf.set_char_p)(cmd, ch);
1776 return;
1777 # endif
1778 }
1779
1780 #endif /* FREETYPE */
1781
1782
1783 static setcharRetvalT
1784 set_no_char(
1785 #ifdef TEXXET
1786 wide_ubyte cmd,
1787 #endif
1788 wide_ubyte ch)
1789 {
1790 if (currinf.virtual) {
1791 currinf.fontp = currinf.virtual->first_font;
1792 if (currinf.fontp != NULL) {
1793 maxchar = currinf.fontp->maxchar;
1794 currinf.set_char_p = currinf.fontp->set_char_p;
1795 #ifdef TEXXET
1796 (*currinf.set_char_p) (cmd, ch);
1797 return;
1798 #else
1799 return (*currinf.set_char_p) (ch);
1800 #endif
1801 }
1802 }
1803 dvi_fmt_error("set_no_char: attempt to set character of unknown font");
1804 exit(0);
1805 /* NOTREACHED */
1806 }
1807
1808
1809 /*
1810 * Set rule. Arguments are coordinates of lower left corner.
1811 */
1812
1813 static void
1814 set_rule(int h, int w)
1815 {
1816 #ifdef TEXXET
1817 put_rule(PXL_H - (currinf.dir < 0 ? w - 1 : 0), PXL_V - h + 1,
1818 (unsigned int)w, (unsigned int)h);
1819 #else
1820 put_rule(PXL_H, PXL_V - h + 1, (unsigned int)w, (unsigned int)h);
1821 #endif
1822 }
1823
1824
1825 /*
1826 * Interpret a sequence of dvi bytes (either the page from the dvi file,
1827 * or a character from a virtual font).
1828 */
1829
1830 static void
1831 draw_part(FILE *fp, struct frame *minframe, double current_dimconv)
1832 {
1833 ubyte ch = 0;
1834 #ifdef TEXXET
1835 struct drawinf oldinfo;
1836 wide_ubyte oldmaxchar = 0;
1837 off_t file_pos = 0;
1838 int refl_count = 0;
1839 #endif
1840 int pause_cnt = 0;
1841
1842 globals.pausing.flag = False;
1843
1844 currinf.fontp = NULL;
1845 currinf.set_char_p = set_no_char;
1846 #ifdef TEXXET
1847 currinf.dir = 1;
1848 scan_frame = NULL; /* indicates we're not scanning */
1849 #endif
1850
1851 for (;;) {
1852 ch = xone(fp);
1853 if (globals.debug & DBG_DVI) {
1854 print_dvi(ch);
1855 }
1856 if (ch <= (ubyte)(SETCHAR0 + 127)) {
1857 #ifdef TEXXET
1858 (*currinf.set_char_p) (ch, ch);
1859 #else
1860 DVI_H += (*currinf.set_char_p) (ch);
1861 #endif
1862 }
1863 else if (FNTNUM0 <= ch && ch <= (ubyte) (FNTNUM0 + 63)) {
1864 change_font((unsigned long)(ch - FNTNUM0));
1865 }
1866 else {
1867 long a, b;
1868
1869 switch (ch) {
1870 case SET1:
1871 case PUT1:
1872 #ifdef TEXXET
1873 (*currinf.set_char_p) (ch, xone(fp));
1874 #else
1875 a = (*currinf.set_char_p) (xone(fp));
1876 if (ch != PUT1)
1877 DVI_H += a;
1878 #endif
1879 break;
1880
1881 case SET2:
1882 case PUT2:
1883 if (!resource.omega)
1884 dvi_fmt_error("%s:%d: draw_part: op-code %d only works with the \"-omega\" option",
1885 __FILE__, __LINE__, ch);
1886 else {
1887 #ifdef TEXXET
1888 (*currinf.set_char_p) (ch, xnum(fp, 2));
1889 #else
1890 a = (*currinf.set_char_p) (xnum(fp, 2));
1891 if (ch != PUT2)
1892 DVI_H += a;
1893 #endif
1894 }
1895 break;
1896
1897 case SETRULE:
1898 /* Be careful, dvicopy outputs rules with
1899 height = 0x80000000. We don't want any
1900 SIGFPE here. */
1901 a = xsfour(fp);
1902 b = xspell_conv(xsfour(fp));
1903 if (a > 0 && b > 0
1904 #ifdef TEXXET
1905 && scan_frame == NULL
1906 #endif
1907 ) {
1908 set_rule(pixel_round(xspell_conv(a)), pixel_round(b));
1909 }
1910 DVI_H += DIR * b;
1911 break;
1912
1913 case PUTRULE:
1914 a = xspell_conv(xsfour(fp));
1915 b = xspell_conv(xsfour(fp));
1916 if (a > 0 && b > 0
1917 #ifdef TEXXET
1918 && scan_frame == NULL
1919 #endif
1920 ) {
1921 set_rule(pixel_round(a), pixel_round(b));
1922 }
1923 break;
1924
1925 case NOP:
1926 break;
1927
1928 case BOP:
1929 xskip(fp, (long)11 * 4);
1930 DVI_H = OFFSET_X;
1931 DVI_V = OFFSET_Y;
1932 PXL_V = pixel_conv(DVI_V);
1933 WW = XX = YY = ZZ = 0;
1934 break;
1935
1936 case EOP:
1937 if (current_frame != minframe)
1938 dvi_fmt_error("%s:%d: draw_part: stack not empty at EOP", __FILE__, __LINE__);
1939 return;
1940
1941 case PUSH:
1942 if (current_frame->next == NULL) {
1943 struct frame *newp = xmalloc(sizeof *newp);
1944
1945 current_frame->next = newp;
1946 newp->prev = current_frame;
1947 newp->next = NULL;
1948 }
1949 current_frame = current_frame->next;
1950 current_frame->data = currinf.data;
1951 break;
1952
1953 case POP:
1954 if (current_frame == minframe)
1955 dvi_fmt_error("%s:%d: draw_part: more POPs than PUSHes", __FILE__, __LINE__);
1956 currinf.data = current_frame->data;
1957 current_frame = current_frame->prev;
1958 break;
1959
1960 #ifdef TEXXET
1961 case SREFL:
1962 if (scan_frame == NULL) {
1963 /* we're not scanning: save some info. */
1964 oldinfo = currinf;
1965 oldmaxchar = maxchar;
1966 if (!currinf.virtual)
1967 file_pos = xtell(fp, currinf.pos);
1968 scan_frame = current_frame; /* now we're scanning */
1969 refl_count = 0;
1970 break;
1971 }
1972 /* we are scanning */
1973 if (current_frame == scan_frame)
1974 ++refl_count;
1975 break;
1976
1977 case EREFL:
1978 if (scan_frame != NULL) { /* if we're scanning */
1979 if (current_frame == scan_frame && --refl_count < 0) {
1980 /* we've hit the end of our scan */
1981 scan_frame = NULL;
1982 /* first: push */
1983 if (current_frame->next == NULL) {
1984 struct frame *newp = xmalloc(sizeof *newp);
1985
1986 current_frame->next = newp;
1987 newp->prev = current_frame;
1988 newp->next = NULL;
1989 }
1990 current_frame = current_frame->next;
1991 current_frame->data = currinf.data;
1992 /* next: restore old file position, XX, etc. */
1993 if (!currinf.virtual) {
1994 off_t bgn_pos = xtell(fp, G_dvi_buf_ptr);
1995
1996 if (file_pos >= bgn_pos) {
1997 oldinfo.pos = dvi_buffer + (file_pos - bgn_pos);
1998 oldinfo.end = currinf.end;
1999 }
2000 else {
2001 (void)lseek(fileno(fp), file_pos, SEEK_SET);
2002 oldinfo.pos = oldinfo.end;
2003 }
2004 }
2005 currinf = oldinfo;
2006 maxchar = oldmaxchar;
2007 /* and then: recover position info. */
2008 DVI_H = current_frame->data.dvi_h;
2009 DVI_V = current_frame->data.dvi_v;
2010 PXL_V = current_frame->data.pxl_v;
2011 /* and finally, reverse direction */
2012 currinf.dir = -currinf.dir;
2013 }
2014 break;
2015 }
2016 /* we're not scanning, */
2017 /* so just reverse direction and then pop */
2018 currinf.dir = -currinf.dir;
2019 currinf.data = current_frame->data;
2020 current_frame = current_frame->prev;
2021 break;
2022 #endif /* TEXXET */
2023
2024 case RIGHT1:
2025 case RIGHT2:
2026 case RIGHT3:
2027 case RIGHT4:
2028 DVI_H += DIR * xspell_conv(xsnum(fp, ch - RIGHT1 + 1));
2029 break;
2030
2031 case W1:
2032 case W2:
2033 case W3:
2034 case W4:
2035 WW = xspell_conv(xsnum(fp, ch - W0));
2036 case W0:
2037 DVI_H += DIR * WW;
2038 break;
2039
2040 case X1:
2041 case X2:
2042 case X3:
2043 case X4:
2044 XX = xspell_conv(xsnum(fp, ch - X0));
2045 case X0:
2046 DVI_H += DIR * XX;
2047 break;
2048
2049 case DOWN1:
2050 case DOWN2:
2051 case DOWN3:
2052 case DOWN4:
2053 DVI_V += xspell_conv(xsnum(fp, ch - DOWN1 + 1));
2054 PXL_V = pixel_conv(DVI_V);
2055 break;
2056
2057 case Y1:
2058 case Y2:
2059 case Y3:
2060 case Y4:
2061 YY = xspell_conv(xsnum(fp, ch - Y0));
2062 case Y0:
2063 DVI_V += YY;
2064 PXL_V = pixel_conv(DVI_V);
2065 break;
2066
2067 case Z1:
2068 case Z2:
2069 case Z3:
2070 case Z4:
2071 ZZ = xspell_conv(xsnum(fp, ch - Z0));
2072 case Z0:
2073 DVI_V += ZZ;
2074 PXL_V = pixel_conv(DVI_V);
2075 break;
2076
2077 case FNT1:
2078 case FNT2:
2079 case FNT3:
2080 case FNT4:
2081 change_font(xnum(fp, ch - FNT1 + 1));
2082 break;
2083
2084 case XXX1:
2085 case XXX2:
2086 case XXX3:
2087 case XXX4:
2088 a = xnum(fp, ch - XXX1 + 1);
2089 if (a > 0) {
2090 char *p = read_special(fp, a);
2091 if (resource.pause && strcmp(p, resource.pause_special) == 0) {
2092 if (++pause_cnt > globals.pausing.num) {
2093 globals.pausing.flag = True;
2094 /* can't use longjmp(globals.ev.canit, 1); */
2095 return;
2096 }
2097 }
2098 applicationDoSpecial(p, a);
2099 }
2100 break;
2101
2102 case FNTDEF1:
2103 case FNTDEF2:
2104 case FNTDEF3:
2105 case FNTDEF4:
2106 xskip(fp, (long)(12 + ch - FNTDEF1 + 1));
2107 a = (long)xone(fp);
2108 xskip(fp, a + (long)xone(fp));
2109 break;
2110
2111 #ifndef TEXXET
2112 case SREFL:
2113 case EREFL:
2114 #endif
2115 case PRE:
2116 case POST:
2117 case POSTPOST:
2118 dvi_fmt_error("%s:%d: draw_part: shouldn't happen: %s encountered",
2119 __FILE__, __LINE__, dvi_table2[ch - (FNTNUM0 + 64)]);
2120 break;
2121
2122 default:
2123 dvi_fmt_error("%s:%d: draw_part: unknown op-code %d", __FILE__, __LINE__, ch);
2124 } /* end switch */
2125 } /* end else (ch not a SETCHAR or FNTNUM) */
2126 } /* end for */
2127 }
2128
2129 extern int waiting_for_anchor;
2130
2131 static void
2132 warn_raw_postscript(void)
2133 {
2134 static int *pagelist = NULL;
2135 static int pagelist_size = 0;
2136 #ifdef PS
2137 if (!resource.hush_stdout) {
2138 if (total_pages >= pagelist_size) {
2139 pagelist_size = total_pages + 1;
2140 pagelist = xrealloc(pagelist, pagelist_size * sizeof *pagelist);
2141 memset(pagelist, 0, pagelist_size * sizeof *pagelist);
2142 }
2143
2144 ASSERT(pagelist_size > current_page, "pagelist_size too small");
2145 if (pagelist[current_page] == 0) {
2146 XDVI_WARNING((stderr, "Raw Postscript commands on page %d may be rendered incorrectly.",
2147 current_page + 1));
2148 pagelist[current_page] = 1;
2149 }
2150
2151 /* too likely to overdraw important information */
2152 /* statusline_info(STATUS_MEDIUM, */
2153 /* "Warning: Postscript commands on this page may not display correctly."); */
2154 }
2155 #endif /* PS */
2156 }
2157
2158 void
2159 draw_page(void)
2160 {
2161 #if 0
2162 volatile double save_gamma = 0.0;
2163 #endif /* 0 */
2164 /* Check for changes in dvi file. */
2165 if (dvi_file_changed()) {
2166 return;
2167 }
2168
2169 if (globals.dvi_file.bak_fp == NULL) {
2170 return;
2171 }
2172
2173 #ifdef PS
2174 have_raw_postscript = False;
2175 #endif
2176
2177 #if COLOR
2178 color_bottom = &fg_initial;
2179 color_bot_size = 1;
2180
2181 if (page_colors.stack != NULL && current_page > 0) {
2182 color_bottom = page_colors.stack[current_page - 1].colorstack;
2183 color_bot_size = page_colors.stack[current_page - 1].stacksize;
2184 }
2185 rcs_top = NULL;
2186 ASSERT(color_bot_size > 0, "color_bot_size mustn't become negative!");
2187 ASSERT(color_bottom != NULL, "color_bottom mustn't become negative!");
2188 set_fg_color(&color_bottom[color_bot_size - 1]);
2189 #endif /* COLOR */
2190
2191 #if !FIXED_FLUSHING_PAGING
2192 draw_border(-currwin.base_x, -currwin.base_y,
2193 ROUNDUP(pageinfo_get_page_width(current_page), currwin.shrinkfactor) + 2,
2194 ROUNDUP(pageinfo_get_page_height(current_page), currwin.shrinkfactor) + 2, globals.gc.high);
2195 #endif /* MOTIF */
2196
2197 if (resource.grid_mode > 0) { /* grid is wanted */
2198 put_grid(-currwin.base_x, -currwin.base_y,
2199 /* ROUNDUP(globals.page.unshrunk_w, currwin.shrinkfactor) + 2, */
2200 /* ROUNDUP(globals.page.unshrunk_h, currwin.shrinkfactor) + 2, */
2201 ROUNDUP(pageinfo_get_page_width(current_page), currwin.shrinkfactor) + 2,
2202 ROUNDUP(pageinfo_get_page_height(current_page), currwin.shrinkfactor) + 2,
2203 ROUNDUP(globals.grid_paper_unit, currwin.shrinkfactor),
2204 globals.gc.high);
2205 }
2206
2207 (void) lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(current_page), SEEK_SET);
2208
2209 memset((char *)&currinf.data, '\0', sizeof currinf.data);
2210 currinf.tn_table_len = TNTABLELEN;
2211 currinf.tn_table = tn_table;
2212 currinf.tn_head = tn_head;
2213 currinf.pos = currinf.end = dvi_buffer;
2214 currinf.virtual = NULL;
2215 dvi_pointer_frame = &currinf;
2216 drawing_mag = (currwin.win == magnifier.win);
2217 psfig_begun = False;
2218
2219 htex_initpage(False, False, current_page);
2220
2221 if (currwin.win == mane.win) {
2222 XRectangle rect;
2223 rect.x = globals.win_expose.min_x;
2224 rect.y = globals.win_expose.min_y;
2225 rect.width = globals.win_expose.max_x - globals.win_expose.min_x;
2226 rect.height = globals.win_expose.max_y - globals.win_expose.min_y;
2227 /* fprintf(stderr, "clip: %d, %d, %d, %d\n", */
2228 /* globals.win_expose.min_x, */
2229 /* globals.win_expose.min_y, */
2230 /* globals.win_expose.max_x - globals.win_expose.min_x, */
2231 /* globals.win_expose.max_y - globals.win_expose.min_y); */
2232 #define SET_CLIP(gc) if (gc != NULL) XSetClipRectangles(DISP, gc, 0, 0, &rect, 1, Unsorted)
2233 #define CLEAR_CLIP(gc) if (gc != NULL) XSetClipMask(DISP, gc, None)
2234 /* Set clip masks for all GC's */
2235 SET_CLIP(globals.gc.fore);
2236 SET_CLIP(globals.gc.fore2);
2237 SET_CLIP(globals.gc.fore2_bak);
2238 SET_CLIP(globals.gc.fore2_bak1);
2239 SET_CLIP(globals.gc.rule);
2240 SET_CLIP(globals.gc.high);
2241 SET_CLIP(globals.gc.linkcolor);
2242 SET_CLIP(globals.gc.copy);
2243 }
2244
2245 if (!setjmp(globals.ev.canit)) {
2246 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BUG ALERT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2247
2248 ALL GLYPH DRAWING/RULE SETTING COMMANDS THAT MIGHT INVOKE
2249 longjmp(globals.ev.canit)
2250 MUST GO INSIDE THIS IF CASE, AND MUST NOT BE INVOKED FROM
2251 SOMEWHERE ELSE!
2252
2253 Failure to check whether a command could (indirectly) invoke
2254 such a drawing routine (like e.g. put_rule()) will result
2255 in *really* strange bugs (see e.g. #616920, and probably also #471021).
2256
2257 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BUG ALERT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2258 */
2259 /* generate an expose event */
2260 if (search_have_match(current_page)) {
2261 search_erase_highlighting(False);
2262 }
2263 if (globals.curr_mode == TEXT_MODE_ACTIVE) {
2264 text_change_region(TEXT_SEL_ERASE, NULL);
2265 }
2266 draw_part(globals.dvi_file.bak_fp, current_frame = &frame0, dimconv);
2267
2268 if (have_raw_postscript) {
2269 warn_raw_postscript();
2270 }
2271 }
2272 else {
2273 /* If we were interrupted, put the expose event back, so that the
2274 * region gets redrawn. The if statement is necessary because the
2275 * magnifier may have been destroyed as part of the interrupt. */
2276 if (currwin.win == mane.win || currwin.win == magnifier.win) {
2277 expose(currwin.win == mane.win ? &mane : &magnifier,
2278 globals.win_expose.min_x - currwin.base_x, globals.win_expose.min_y - currwin.base_y,
2279 globals.win_expose.max_x - globals.win_expose.min_x, globals.win_expose.max_y - globals.win_expose.min_y);
2280 }
2281
2282 #ifdef PS
2283 psp.interrupt();
2284 #endif
2285 globals.ev.flags &= ~EV_MAG_GONE;
2286 #if 0
2287 if (search_have_match() && save_gamma != 0.0) {
2288 resource.gamma = save_gamma;
2289 do_color_change();
2290 reset_fonts();
2291 }
2292 #endif /* 0 */
2293 }
2294
2295 drawing_mag = False;
2296 dvi_pointer_frame = NULL;
2297 if (currwin.win == mane.win) {
2298 if (globals.src.fwd_box_page >= 0) {
2299 source_fwd_draw_box(); /* draw box showing found source line */
2300 }
2301 htex_draw_anchormarkers();
2302 }
2303 #ifdef PS
2304 psp.endpage();
2305 #endif
2306 if (currwin.win == mane.win && resource.postscript != 1) {
2307 display_bboxes();
2308 }
2309 if (search_have_match(current_page)) {
2310 /* highlight search match */
2311 search_draw_inverted_regions();
2312 }
2313 if (currwin.win == mane.win && (globals.curr_mode == TEXT_MODE_ACTIVE)) {
2314 /* highlight selection */
2315 text_change_region(TEXT_SEL_REDRAW, NULL);
2316 }
2317 if (globals.curr_mode == RULER_MODE_ACTIVE) {
2318 redraw_ruler();
2319 }
2320
2321 clear_bboxes();
2322
2323 if (currwin.win == mane.win) {
2324 CLEAR_CLIP(globals.gc.fore);
2325 CLEAR_CLIP(globals.gc.fore2);
2326 CLEAR_CLIP(globals.gc.fore2_bak);
2327 CLEAR_CLIP(globals.gc.fore2_bak1);
2328 CLEAR_CLIP(globals.gc.rule);
2329 CLEAR_CLIP(globals.gc.high);
2330 CLEAR_CLIP(globals.gc.linkcolor);
2331 CLEAR_CLIP(globals.gc.copy);
2332 }
2333 #undef SET_CLIP
2334 #undef CLEAR_CLIP
2335
2336 currwin.win = (Window) 0;
2337 }
2338
2339 /* this sets the file-scope htex_anchor_type to the type of the anchor just scanned,
2340 which is used by the drawing routines to determine whether current position is inside
2341 an anchor. Called from special.c.
2342 */
2343 void
2344 htex_do_special(const char *str, size_t len)
2345 {
2346 if (INSIDE_MANE_WIN) {
2347 htex_inside_href = htex_scan_anchor(str, len);
2348 }
2349 }
2350
2351 /*
2352 * General dvi scanning routines. These are used for:
2353 * o source special lookups and
2354 * o finding the dimensions of links (if compiling with support for
2355 * hypertext specials).
2356 * This routine can be a bit slower than draw_page()/draw_part(), since
2357 * it is not run that often; that is why it is a separate routine in
2358 * spite of much duplication.
2359 *
2360 * Note that it does not use a separate copy of define_font().
2361 */
2362
2363 /*
2364 * This set of routines can be called while draw_part() is active,
2365 * so the global variables must be separate.
2366 */
2367
2368 static struct frame geom_frame0; /* dummy head of list */
2369
2370 #ifdef TEXXET
2371 static struct frame *geom_scan_frame; /* head frame for scanning */
2372 #endif
2373
2374 static struct frame *geom_current_frame;
2375
2376 static uint32_t
2377 get_unicode_char(wide_ubyte ch, struct drawinf currinf, char *retbuf)
2378 {
2379 #if FREETYPE
2380 /* attempt 1: if it's a Type1 font, get the Adobe character name
2381 from FreeType's FT_Get_Glyph_Name */
2382 if (currinf.fontp->ft != NULL) {
2383 FT_Face face = currinf.fontp->ft->face;
2384
2385 ASSERT(face != NULL, "Font must have been loaded at this point!");
2386 if (FT_Has_PS_Glyph_Names(face)) {
2387 char buffer[32]; /* holds longest name in adobe2unicode_table */
2388 int index = currinf.fontp->glyph[ch].addr;
2389
2390 if (index == 0
2391 || FT_Get_Glyph_Name(face, index, buffer, sizeof buffer) != 0)
2392 TRACE_FIND_VERBOSE((stderr, "T1 char: %d has no name", ch));
2393 else {
2394 TRACE_FIND_VERBOSE((stderr,
2395 "T1 char: %d = `%s' (font: %s; enc: %s)",
2396 ch, char_name,
2397 currinf.fontp->fontname,
2398 currinf.fontp->ft->t1->encname));
2399 return adobe2unicode_name(buffer);
2400 }
2401 }
2402 }
2403 #endif /* FREETYPE */
2404 /* attempt 2: try to derive the encoding from the font name,
2405 by looking it up in a list of known font names.
2406 */
2407 return guess_encoding(ch, currinf.fontp->fontname, retbuf);
2408 }
2409
2410 #define MAX_CHARS 16 /* maximum number of unicode characters that one do_char can produce (rounded up ...) */
2411 static const size_t ALLOC_STEP = 1024;
2412
2413 /* create a page break at BOP, unless we're at the start of the file */
2414 static void
2415 do_newpage(struct scan_info *info)
2416 {
2417 struct word_info *w_info = (struct word_info *)info->data;
2418 if (w_info->curr_buf_idx > 0) { /* not at start of file */
2419 /* resize buffer if needed */
2420 while (w_info->curr_buf_idx + MAX_CHARS >= w_info->txt_buf_size) {
2421 w_info->txt_buf_size += ALLOC_STEP;
2422 w_info->txt_buf = xrealloc(w_info->txt_buf, w_info->txt_buf_size);
2423 }
2424 w_info->txt_buf[w_info->curr_buf_idx++] = '\n';
2425 }
2426 }
2427
2428 static void
2429 reset_bboxes(struct word_info *info)
2430 {
2431 size_t i;
2432 for (i = 0; i < info->bboxes_size; i++) {
2433 info->bboxes[i].ulx = INT_MAX;
2434 info->bboxes[i].uly = INT_MAX;
2435 info->bboxes[i].lrx = 0;
2436 info->bboxes[i].lry = 0;
2437 }
2438 }
2439
2440 static void
2441 delete_last_bbox(struct word_info *info)
2442 {
2443 info->bboxes[info->bboxes_idx].ulx = INT_MAX;
2444 info->bboxes[info->bboxes_idx].uly = INT_MAX;
2445 info->bboxes[info->bboxes_idx].lrx = 0;
2446 info->bboxes[info->bboxes_idx].lry = 0;
2447 }
2448
2449 static void
2450 finish_bbox(struct word_info *info)
2451 {
2452 /* if (info->bboxes_idx == 0) /\* nothing to do *\/ */
2453 /* return; */
2454
2455 info->bboxes_idx++;
2456 while (info->bboxes_idx + 1 > info->bboxes_size) {
2457 /* re-allocate info */
2458 info->bboxes_size += 32;
2459 info->bboxes = xrealloc(info->bboxes, info->bboxes_size * sizeof *(info->bboxes));
2460 }
2461 info->bboxes[info->bboxes_idx].ulx = INT_MAX;
2462 info->bboxes[info->bboxes_idx].uly = INT_MAX;
2463 info->bboxes[info->bboxes_idx].lrx = 0;
2464 info->bboxes[info->bboxes_idx].lry = 0;
2465
2466 #if 0
2467 fprintf(stderr, "========= finish_bbox: index=%d, boxes:\n", info->bboxes_idx);
2468 {
2469 int i;
2470 for (i = 0; i < info->bboxes_idx; i++) {
2471 fprintf(stderr, "%d: x %d, y %d, w %d, h %d\n",
2472 i, info->bboxes[i].ulx, info->bboxes[i].uly, info->bboxes[i].lrx, info->bboxes[i].lry);
2473 }
2474 }
2475 #endif /* 0 */
2476 }
2477
2478 static void
2479 create_bbox(struct word_info *info, int x, int y, int w, int h)
2480 {
2481 #if 0
2482 fprintf(stderr, "++++++++++++++ inside match: %d,%d,%d,%d!\n", x,y,w,h);
2483 XDrawRectangle(DISP, mane.win, globals.gc.high,
2484 x / (double)currwin.shrinkfactor + 0.5,
2485 y / (double)currwin.shrinkfactor + 0.5,
2486 w / (double)currwin.shrinkfactor + 0.5,
2487 h / (double)currwin.shrinkfactor + 0.5);
2488 #endif
2489 while (info->bboxes_idx + 1 > info->bboxes_size) {
2490 /* re-allocate info */
2491 size_t old_size = info->bboxes_size;
2492 size_t i;
2493 info->bboxes_size += 32;
2494 info->bboxes = xrealloc(info->bboxes, info->bboxes_size * sizeof *(info->bboxes));
2495 for (i = old_size; i < info->bboxes_size; i++) {
2496 info->bboxes[i].ulx = INT_MAX;
2497 info->bboxes[i].uly = INT_MAX;
2498 info->bboxes[i].lrx = 0;
2499 info->bboxes[i].lry = 0;
2500 }
2501
2502 }
2503 /* adjust size of box */
2504 if (x < info->bboxes[info->bboxes_idx].ulx)
2505 info->bboxes[info->bboxes_idx].ulx = x;
2506 if (y < info->bboxes[info->bboxes_idx].uly)
2507 info->bboxes[info->bboxes_idx].uly = y;
2508 if (x + w > info->bboxes[info->bboxes_idx].lrx)
2509 info->bboxes[info->bboxes_idx].lrx = x + w;
2510 if (y + h > info->bboxes[info->bboxes_idx].lry)
2511 info->bboxes[info->bboxes_idx].lry = y + h;
2512 #if 0
2513 fprintf(stderr, "dimens: %d, %d, %d, %d\n",
2514 info->bboxes[info->bboxes_idx].ulx,
2515 info->bboxes[info->bboxes_idx].uly,
2516 info->bboxes[info->bboxes_idx].lrx,
2517 info->bboxes[info->bboxes_idx].lry);
2518 fprintf(stderr, "============ create_bbox: index=%d, boxes:\n", info->bboxes_idx);
2519 {
2520 int i;
2521 for (i = 0; i <= info->bboxes_idx; i++) {
2522 fprintf(stderr, "%d: x %d, y %d, w %d, h %d\n",
2523 i, info->bboxes[i].ulx, info->bboxes[i].uly, info->bboxes[i].lrx, info->bboxes[i].lry);
2524 }
2525 }
2526 #endif /* 0 */
2527 }
2528
2529 static void
2530 map_index_positions(const struct search_info *searchinfo,
2531 const struct page_mapping *page_mapping,
2532 int *from, int *to)
2533 {
2534 *from = searchinfo->from_pos;
2535 *to = searchinfo->to_pos;
2536
2537 if (*from >= page_mapping[0].offset) { /* on second page of scan */
2538 #if 0
2539 fprintf(stderr, "current_page: %d, from: %d, to: %d; subtracting %d for pageinfo at index 0\n",
2540 current_page, *from, *to, page_mapping[0].offset);
2541 #endif /* 0 */
2542 if (page_mapping[0].offset != -1) {
2543 ASSERT(page_mapping[0].offset != -1, "page_mapping not properly initialized?");
2544 (*from) -= page_mapping[0].offset;
2545 (*to) -= page_mapping[0].offset;
2546 }
2547 ASSERT(*from >= 0, "index must be > 0");
2548 ASSERT(*to >= 0, "index must be > 0");
2549
2550 }
2551 }
2552
2553 /* decrement curr_buf_idx so that the previous character in info
2554 * (which might be a multi-byte character) is erased.
2555 */
2556 static void
2557 erase_prev_char(struct word_info *info)
2558 {
2559 /* erase the hyphen, which might be a multibyte character */
2560 if ((unsigned char)(info->txt_buf[info->curr_buf_idx - 1]) < 0x80) { /* single-byte */
2561 TRACE_FIND_VERBOSE((stderr, "1 XXX: %d",
2562 (unsigned char)(info->txt_buf[info->curr_buf_idx - 1])));
2563 info->txt_buf[--(info->curr_buf_idx)] = '\0';
2564 }
2565 else { /* skip back over multi-byte sequence */
2566 while ((unsigned char)(info->txt_buf[info->curr_buf_idx - 1]) >= 0x80
2567 && (unsigned char)(info->txt_buf[info->curr_buf_idx - 1]) < 0xC0) { /* 10xxxxxx bytes */
2568 TRACE_FIND_VERBOSE((stderr, "2 XXX: %d",
2569 (unsigned char)(info->txt_buf[info->curr_buf_idx - 1])));
2570 info->txt_buf[--(info->curr_buf_idx)] = '\0';
2571 }
2572 /* first byte (TODO: sanity check?) */
2573 info->txt_buf[--(info->curr_buf_idx)] = '\0';
2574 }
2575 }
2576
2577
2578 static Boolean
2579 inside_text_match(int curr_pos, int from, int to)
2580 {
2581 return from < curr_pos && curr_pos <= to;
2582 }
2583
2584 static Boolean reinit_scan = False;
2585
2586 void
2587 reinit_text_scan(void) {
2588 reinit_scan = True;
2589 }
2590
2591 static Boolean
2592 inside_bbox(int x, int y, int w, int h,
2593 struct bbox *bboxes)
2594 {
2595 int mid_x, mid_y;
2596 #if 0
2597 XDrawRectangle(DISP, mane.win, globals.gc.high,
2598 x / (double)currwin.shrinkfactor + 0.5,
2599 y / (double)currwin.shrinkfactor + 0.5,
2600 w / (double)currwin.shrinkfactor + 0.5,
2601 h / (double)currwin.shrinkfactor + 0.5);
2602 #endif
2603 /* already treat it as inside if there's an overlap of more than 50% */
2604 mid_x = x + w / 2.0;
2605 mid_y = y + h / 2.0;
2606 #if 0
2607 fprintf(stderr, "------- check:\n%d, %d\n%d, %d, %d, %d\n",
2608 mid_x, mid_y, bboxes->ulx, bboxes->uly, bboxes->lrx, bboxes->lry);
2609 #endif
2610 if (bboxes->ulx <= mid_x &&
2611 bboxes->uly <= mid_y &&
2612 bboxes->lrx >= mid_x &&
2613 bboxes->lry >= mid_y) {
2614 #if 0
2615 fprintf(stderr, "====== MATCH:\n%d, %d\n%d, %d, %d, %d\n",
2616 mid_x, mid_y, bboxes->ulx, bboxes->uly, bboxes->lrx, bboxes->lry);
2617 #endif
2618 return True;
2619 }
2620 return False;
2621 }
2622
2623 static Boolean
2624 inside_bbox_line(int y, int h,
2625 struct bbox *bboxes)
2626 {
2627 int mid_y = y + h / 2.0;
2628 if (bboxes->uly <= mid_y &&
2629 bboxes->lry >= mid_y) {
2630 #if 0
2631 fprintf(stderr, "MATCH:\n%d, %d, %d, %d\n%d, %d, %d, %d\n",
2632 x, y, w, h, bboxes->ulx, bboxes->uly, bboxes->lrx, bboxes->lry);
2633 #endif
2634 return True;
2635 }
2636 return False;
2637 }
2638
2639 static void
2640 do_char(wide_ubyte ch,
2641 struct drawinf currinf,
2642 struct scan_info *info,
2643 long pxl_v1, long pxl_v2,
2644 long x1, long x2,
2645 struct glyph *g)
2646 {
2647 struct word_info *w_info = (struct word_info *)info->data;
2648 struct search_settings *settings = NULL;
2649 const struct page_mapping *page_mapping = NULL;
2650 uint32_t u_glyph = 0, new_glyph = 0;
2651 Boolean convert_to_lowercase = False;
2652 /* if set to true, ignore whitespace before/after character (for CJK characters) */
2653 Boolean ignore_whitespace = False;
2654
2655 /* default min space between words, in DVI units */
2656 /* TODO: for T1 fonts, should we look at fontdimen2 / fontdimen4?
2657 (e.g. tfminfo[fontmaps[currinf.fontp->t1id].tfmidx].fontdimen2)
2658 */
2659 long min_delta = (int)(1.5 * currinf.fontp->pixsize + 0.5) << 16;
2660
2661 size_t buf_offset = w_info->buffer_offset;
2662
2663 size_t i;
2664 /* for delayed insertion of newlines in text selection mode */
2665 static Boolean had_newline = False;
2666 /* for whitespace insertion in text selection mode */
2667 static Boolean had_chars_in_line = False;
2668 static long last_dvi_h1 = 0, last_pxl_v = 0;
2669 static long last_dvi_h2 = 0;
2670 static long last_x = 0;
2671 static uint32_t last_u_glyph = 0;
2672 static int last_page = -1;
2673 static int page_bak = -1;
2674 const char *expanded_lig = NULL;
2675 const uint32_t UNKNOWN_GLYPH_CODE = 0x003F; /* question mark */
2676 char retbuf[MAX_CHARS];
2677 retbuf[0] = '\0';
2678
2679 if (w_info != NULL && w_info->settings != NULL) {
2680 settings = w_info->settings;
2681 if (!settings->case_sensitive)
2682 convert_to_lowercase = True;
2683 }
2684
2685 if (w_info->bbox_pass) {
2686 ASSERT(w_info != NULL, "");
2687 ASSERT(w_info->page_mapping != NULL, "");
2688 page_mapping = w_info->page_mapping;
2689 }
2690
2691 if (reinit_scan || last_page != current_page) { /* reinit */
2692 last_dvi_h1 = last_pxl_v = last_dvi_h2 = last_x = 0;
2693 last_page = current_page;
2694 last_u_glyph = 0;
2695 w_info->bboxes_idx = 0;
2696 reset_bboxes(w_info);
2697 /* w_info->curr_buf_idx = 0; */
2698 reinit_scan = False;
2699 had_newline = False;
2700 had_chars_in_line = False;
2701 }
2702
2703 /* TRACE_FIND((stderr, "\n--------- POSITIONS: %ld, %ld, %ld, %ld", x1, pxl_v2, x2, y2)); */
2704
2705 if ((u_glyph = get_unicode_char(ch, currinf, retbuf)) == 0) {
2706 if (retbuf[0] == '\0') {
2707 TRACE_FIND((stderr, "unknown glyph `%lu'\n", (unsigned long)ch));
2708 u_glyph = UNKNOWN_GLYPH_CODE;
2709 }
2710 else /* several characters in retbuf, will be evaluated later */
2711 u_glyph = 0;
2712 }
2713
2714 TRACE_FIND_VERBOSE((stderr, "UNICODE: 0x%.4X; idx: %ld",
2715 (unsigned int)u_glyph,
2716 (unsigned long)w_info->curr_buf_idx));
2717
2718 /* resize buffer if needed */
2719 while (w_info->curr_buf_idx + MAX_CHARS >= w_info->txt_buf_size) {
2720 w_info->txt_buf_size += ALLOC_STEP;
2721 w_info->txt_buf = xrealloc(w_info->txt_buf, w_info->txt_buf_size);
2722 }
2723
2724 /* Copy text into buffer, applying heuristics for special glyphs.
2725 A `\0' is always appended, so that we have a valid C string;
2726 if we have more text, this '\0' will be overwritten in the next call. */
2727
2728 /* apply accent/linebreak heuristics */
2729 /* fprintf(stderr, "Checking: %ld > 0, %ld > 0\n", last_dvi_h1, last_pxl_v); */
2730 if (last_dvi_h1 > 0 && last_pxl_v > 0) { /* had at least 1 character */
2731 TRACE_FIND_VERBOSE((stderr, "++++ dvi_h: %ld, last_dvi_h1: %ld, w_info->curr_buf_idx: %ld",
2732 DVI_H, last_dvi_h1, (unsigned long)w_info->curr_buf_idx));
2733
2734 /* spaces after/before ideographic characters are ignored */
2735 if (is_ideograph(last_u_glyph) || is_ideograph(u_glyph))
2736 ignore_whitespace = true;
2737
2738 /* first, check for linebreaks since accents are also triggered by negative hspace.
2739 * Usually, a linebreak is signalled by vertical movement down. However, in multicolumn
2740 * mode, it can also be a movement up, which we try to catch with the second condition. */
2741 if (pxl_v2 > last_pxl_v + (int)(1.2 * currinf.fontp->pixsize + 0.5)
2742 || (page_bak == current_page && pxl_v2 + (int)(6 * currinf.fontp->pixsize + 0.5) < last_pxl_v)) {
2743 TRACE_FIND_VERBOSE((stderr, "linebreak (%ld > %ld + %d || %ld < %ld)!\n",
2744 pxl_v2, last_pxl_v, (int)(1.2 * currinf.fontp->pixsize + 0.5),
2745 pxl_v2 + (int)(6 * currinf.fontp->pixsize + 0.5), last_pxl_v));
2746
2747 /* remove hyphen followed by newline if ignore_hyphens option is set,
2748 and we're scanning for string search: */
2749 if ((w_info->search_scan_pass || w_info->bbox_pass)
2750 && settings->ignore_hyphens && w_info->curr_buf_idx > 0
2751 && is_hyphenchar(last_u_glyph)) {
2752
2753 erase_prev_char(w_info);
2754 TRACE_FIND_VERBOSE((stderr, "%d > %d + %d? offset: %lu",
2755 settings->searchinfo->from_pos,
2756 (int)w_info->curr_buf_idx,
2757 settings->hyphen_delta, (unsigned long)buf_offset));
2758 if (!w_info->bbox_pass
2759 && settings->searchinfo->from_pos
2760 > (int)(w_info->curr_buf_idx + buf_offset + settings->hyphen_delta)) {
2761 settings->hyphen_delta += 2;
2762 }
2763 TRACE_FIND((stderr, "erasing hyphen %lu at pos %lu with char %c; hyphen_delta is: %d",
2764 (unsigned long)last_u_glyph, (unsigned long)w_info->curr_buf_idx, ch, settings->hyphen_delta));
2765 /* If the position of this hyphen had been at the start of the bounding
2766 box, this bounding box info is now invalid - delete it */
2767 if (w_info->bbox_pass) {
2768 int from, to;
2769 map_index_positions(settings->searchinfo, page_mapping, &from, &to);
2770 if ((int)w_info->curr_buf_idx == from) {
2771 delete_last_bbox(w_info);
2772 }
2773 }
2774 }
2775 else { /* ignore_hyphens not set, insert newline */
2776 /* Also save hyphen_delta, in case we need to update from_pos and to_pos
2777 if user switched from ignore hyphens to don't ignore hyphens */
2778 if (w_info->search_scan_pass && !w_info->bbox_pass
2779 && is_hyphenchar(last_u_glyph)
2780 && settings->searchinfo->from_pos + settings->hyphen_delta
2781 >= (int)(w_info->curr_buf_idx + buf_offset)) {
2782 settings->hyphen_delta += 2;
2783 TRACE_FIND((stderr, "updating delta: %d at pos %d, curr_idx %d, offset: %lu",
2784 settings->hyphen_delta, settings->searchinfo->from_pos,
2785 (int)w_info->curr_buf_idx, (unsigned long)buf_offset));
2786 }
2787 if (w_info->text_selection_pass) {
2788 if (inside_bbox(last_x, last_pxl_v, 5, 5, w_info->bboxes)
2789 || (inside_bbox_line(last_pxl_v, 5, w_info->bboxes))) {
2790 had_newline = True;
2791 had_chars_in_line = False;
2792 }
2793 }
2794 else if (w_info->search_scan_pass && settings->ignore_linebreaks && !ignore_whitespace)
2795 w_info->txt_buf[w_info->curr_buf_idx++] = ' ';
2796 else if (!ignore_whitespace)
2797 w_info->txt_buf[w_info->curr_buf_idx++] = '\n';
2798 }
2799 if (w_info->bbox_pass) {
2800 int from, to;
2801 map_index_positions(settings->searchinfo, page_mapping, &from, &to);
2802 if (inside_text_match((int)w_info->curr_buf_idx, from, to)) {
2803 finish_bbox(w_info);
2804 }
2805 }
2806 }
2807 else if (w_info->curr_buf_idx > 0 && last_u_glyph != 0 && last_x > 0
2808 && (!w_info->text_selection_pass
2809 || (w_info->text_selection_pass
2810 && inside_bbox(last_x, pxl_v2, x1 - last_x, pxl_v1 - pxl_v2, w_info->bboxes)))
2811 && x1 < last_x /* overlapping glyphs: check for diacritics */
2812 && ((new_glyph = get_accented_glyph(last_u_glyph, u_glyph)) != 0)) {
2813 erase_prev_char(w_info);
2814 /* use new glyph for next writing operation: */
2815 u_glyph = new_glyph;
2816 }
2817 else if (!ignore_whitespace && last_dvi_h2 > 0 && DVI_H > last_dvi_h2 + min_delta) {
2818 TRACE_FIND_VERBOSE((stderr, "space (%ld > %ld + %ld)!", DVI_H, last_dvi_h2, min_delta));
2819 if (!w_info->text_selection_pass
2820 || (w_info->text_selection_pass
2821 && had_chars_in_line
2822 && inside_bbox(x1, pxl_v2, x2 - x1, g->bitmap.h, w_info->bboxes))) {
2823 w_info->txt_buf[w_info->curr_buf_idx++] = ' ';
2824 w_info->txt_buf[w_info->curr_buf_idx] = '\0';
2825 if (w_info->bbox_pass) {
2826 int from, to;
2827 map_index_positions(settings->searchinfo, page_mapping, &from, &to);
2828 if (inside_text_match((int)w_info->curr_buf_idx, from, to)) {
2829 TRACE_FIND_VERBOSE((stderr, "bounding box for space: %ld, %ld, %ld, %ld",
2830 last_x, last_pxl_v, x1 - last_x, pxl_v1 - pxl_v2));
2831 create_bbox(w_info, last_x, pxl_v2, x1 - last_x, pxl_v1 - pxl_v2);
2832 }
2833 }
2834 }
2835 }
2836 }
2837
2838 last_page = current_page;
2839 if (retbuf[0] != '\0'
2840 || (expanded_lig = expand_ligature(u_glyph)) != NULL
2841 /* when in search pass, normalize more characters */
2842 || ((w_info->bbox_pass || w_info->search_scan_pass)
2843 && (expanded_lig = search_normalize_chars(u_glyph)) != NULL)) {
2844 /* expanded ligature, which is always in 7-bit ASCII -> copy into buffer */
2845 size_t len;
2846 if (retbuf[0] != '\0')
2847 expanded_lig = retbuf;
2848 len = strlen(expanded_lig);
2849 TRACE_FIND_VERBOSE((stderr, "I: %lu to %lu",
2850 (unsigned long)w_info->curr_buf_idx,
2851 (unsigned long)(w_info->curr_buf_idx + len)));
2852
2853 if (!w_info->text_selection_pass
2854 || (w_info->text_selection_pass && inside_bbox(x1, pxl_v2, x2 - x1, g->bitmap.h,
2855 w_info->bboxes))) {
2856
2857 /* fprintf(stderr, "inserting1 %s\n", expanded_lig); */
2858 if (had_newline) {
2859 w_info->txt_buf[w_info->curr_buf_idx++] = '\n';
2860 had_newline = False;
2861 }
2862 memcpy(w_info->txt_buf + w_info->curr_buf_idx, expanded_lig, len);
2863 had_chars_in_line = True;
2864 for (i = 0; i < len; i++) {
2865 w_info->curr_buf_idx++;
2866 /* fprintf(stderr, "setting index to: %d\n", w_info->curr_buf_idx); */
2867 if (w_info->bbox_pass) {
2868 int from, to;
2869 map_index_positions(settings->searchinfo, page_mapping, &from, &to);
2870 if (inside_text_match((int)w_info->curr_buf_idx, from, to)) {
2871 create_bbox(w_info, x1, pxl_v2, x2 - x1, g->bitmap.h);
2872 }
2873 }
2874 }
2875 w_info->txt_buf[w_info->curr_buf_idx] = '\0';
2876 }
2877 }
2878 else if (!w_info->text_selection_pass
2879 || (w_info->text_selection_pass && inside_bbox(x1, pxl_v2, x2 - x1, g->bitmap.h, w_info->bboxes))) {
2880 /* convert to utf8 */
2881 char utf8_buf[MAX_CHARS]; /* ample ... */
2882 size_t len;
2883 /* convert to utf8, eventually lowercasing */
2884 ucs4_to_utf8(u_glyph, utf8_buf, &len, convert_to_lowercase);
2885 /* fprintf(stderr, "inserting2 %lu\n", u_glyph); */
2886 if (had_newline) {
2887 w_info->txt_buf[w_info->curr_buf_idx++] = '\n';
2888 had_newline = False;
2889 }
2890 memcpy(w_info->txt_buf + w_info->curr_buf_idx, utf8_buf, len);
2891 had_chars_in_line = True;
2892 for (i = 0; i < len; i++) {
2893 w_info->curr_buf_idx++;
2894 /* fprintf(stderr, "setting index2 to: %d\n", w_info->curr_buf_idx); */
2895 if (w_info->bbox_pass) {
2896 int from, to;
2897 #if 0
2898 int j;
2899 fprintf(stderr, "current page_mapping:\n");
2900 for (j = 0; j <= total_pages; j++) {
2901 fprintf(stderr, "%d: %d\n", j, page_mapping[j]);
2902 }
2903 #endif /* 0 */
2904 /* fprintf(stderr, "mapping!\n"); */
2905 map_index_positions(settings->searchinfo, page_mapping, &from, &to);
2906 if (inside_text_match((int)w_info->curr_buf_idx, from, to)) {
2907 create_bbox(w_info, x1, pxl_v2, x2 - x1, g->bitmap.h);
2908 }
2909 }
2910 }
2911 w_info->txt_buf[w_info->curr_buf_idx] = '\0'; /* ensure termination */
2912 }
2913
2914 last_dvi_h1 = DVI_H;
2915 last_u_glyph = u_glyph;
2916 last_pxl_v = pxl_v1;
2917 last_dvi_h2 = DVI_H + currinf.fontp->glyph[ch].dvi_adv;
2918 last_x = x2;
2919 }
2920
2921 /* handle a character in the text scanning routine */
2922 long
2923 text_do_char(FILE *fp, struct scan_info *info, wide_ubyte ch)
2924 {
2925 if (currinf.set_char_p == set_no_char) {
2926 if (currinf.virtual == NULL
2927 || (currinf.fontp = currinf.virtual->first_font) == NULL)
2928 return 0; /* error; we'll catch it later */
2929 maxchar = currinf.fontp->maxchar;
2930 currinf.set_char_p = currinf.fontp->set_char_p;
2931 }
2932
2933 if (currinf.set_char_p == set_empty_char)
2934 return 0; /* error; we'll catch it later */
2935
2936 #if FREETYPE
2937 if (currinf.set_char_p == set_ft_char)
2938 do_load_freetype_font();
2939 #endif
2940
2941 #if FREETYPE
2942 while (currinf.set_char_p == load_n_set_char)
2943 #else
2944 if (currinf.set_char_p == load_n_set_char)
2945 #endif
2946 {
2947 if (globals.ev.flags & EV_GE_NEWDOC) /* if abort */
2948 return 0;
2949 if (!load_font(currinf.fontp
2950 #if DELAYED_MKTEXPK
2951 , True
2952 #endif
2953 )) { /* if not found */
2954 if (globals.ev.flags & EV_GE_NEWDOC) /* if abort */
2955 return 0;
2956
2957 currinf.set_char_p = currinf.fontp->set_char_p = set_empty_char;
2958 warn_setting_empty_char();
2959 return 0;
2960 }
2961 maxchar = currinf.fontp->maxchar;
2962 currinf.set_char_p = currinf.fontp->set_char_p;
2963 #if FREETYPE
2964 if (currinf.set_char_p == set_ft_char)
2965 do_load_freetype_font();
2966 #endif
2967 }
2968
2969 if (currinf.set_char_p == set_char) {
2970 struct glyph *g;
2971 long x, y;
2972
2973 if (ch > maxchar)
2974 return 0; /* catch the error later */
2975 if ((g = &currinf.fontp->glyph[ch])->bitmap.bits == NULL) {
2976 if (g->addr == 0)
2977 return 0; /* catch the error later */
2978 if (g->addr == -1)
2979 return 0; /* previously flagged missing char */
2980 #if FREETYPE
2981 if (currinf.fontp->ft == NULL) /* if not freetype font */
2982 #endif
2983 {
2984 open_font_file(currinf.fontp);
2985 fseek(currinf.fontp->file, g->addr, SEEK_SET);
2986 }
2987 (*currinf.fontp->read_char) (currinf.fontp, ch);
2988 if (globals.debug & DBG_BITMAP)
2989 print_char((ubyte) ch, g);
2990 currinf.fontp->timestamp = ++current_timestamp;
2991 }
2992 #ifdef TEXXET
2993 if (geom_scan_frame == NULL) {
2994 long dvi_h_sav = DVI_H;
2995 if (currinf.dir < 0)
2996 DVI_H -= g->dvi_adv;
2997 #endif
2998 x = G_PXL_H - g->x;
2999 y = PXL_V - g->y;
3000 do_char(ch, currinf, info, PXL_V, y, x, x + g->bitmap.w - 1, g);
3001 #ifdef TEXXET
3002 DVI_H = dvi_h_sav;
3003 }
3004 #endif
3005 return DIR * g->dvi_adv;
3006 }
3007 else if (currinf.set_char_p == set_vf_char) {
3008 struct macro *m;
3009 struct drawinf oldinfo;
3010 wide_ubyte oldmaxchar;
3011 #ifdef TEXXET
3012 long dvi_h_sav;
3013 #endif
3014
3015 if (ch > maxchar)
3016 return 0; /* catch the error later */
3017 if ((m = &currinf.fontp->macro[ch])->pos == NULL)
3018 return 0; /* catch the error later */
3019 #ifdef TEXXET
3020 dvi_h_sav = DVI_H;
3021 if (currinf.dir < 0)
3022 DVI_H -= m->dvi_adv;
3023 if (geom_scan_frame == NULL) {
3024 #endif
3025 oldinfo = currinf;
3026 oldmaxchar = maxchar;
3027 WW = XX = YY = ZZ = 0;
3028 currinf.tn_table_len = VFTABLELEN;
3029 currinf.tn_table = currinf.fontp->vf_table;
3030 currinf.tn_head = currinf.fontp->vf_chain;
3031 currinf.pos = m->pos;
3032 currinf.end = m->end;
3033 currinf.virtual = currinf.fontp;
3034 geom_scan_part(text_do_char, fp, info, geom_current_frame, currinf.fontp->dimconv);
3035 currinf = oldinfo;
3036 maxchar = oldmaxchar;
3037 #ifdef TEXXET
3038 DVI_H = dvi_h_sav;
3039 }
3040 #endif
3041 return DIR * m->dvi_adv;
3042 }
3043 else {
3044 XDVI_FATAL((stderr, "currinf.set_char_p is not a registered routine!"));
3045 }
3046 /* NOTREACHED */
3047 return 0;
3048 }
3049
3050 /*
3051 * Handle a character in geometric scanning routine.
3052 */
3053
3054 static long
3055 geom_do_char(FILE *fp, struct scan_info *info, wide_ubyte ch)
3056 {
3057 struct geom_info *g_info = info->data;
3058
3059 if (currinf.set_char_p == set_no_char) {
3060 if (currinf.virtual == NULL
3061 || (currinf.fontp = currinf.virtual->first_font) == NULL)
3062 return 0; /* error; we'll catch it later */
3063 maxchar = currinf.fontp->maxchar;
3064 currinf.set_char_p = currinf.fontp->set_char_p;
3065 }
3066
3067 if (currinf.set_char_p == set_empty_char)
3068 return 0; /* error; we'll catch it later */
3069
3070 #if FREETYPE
3071 if (currinf.set_char_p == set_ft_char)
3072 do_load_freetype_font();
3073 #endif
3074
3075 #if FREETYPE
3076 while (currinf.set_char_p == load_n_set_char)
3077 #else
3078 if (currinf.set_char_p == load_n_set_char)
3079 #endif
3080 {
3081 if (globals.ev.flags & EV_GE_NEWDOC) /* if abort */
3082 return 0;
3083 if (!load_font(currinf.fontp
3084 #if DELAYED_MKTEXPK
3085 , True
3086 #endif
3087 )) { /* if not found */
3088 if (globals.ev.flags & EV_GE_NEWDOC) /* if abort */
3089 return 0;
3090
3091 currinf.set_char_p = currinf.fontp->set_char_p = set_empty_char;
3092 warn_setting_empty_char();
3093 return 0;
3094 }
3095 maxchar = currinf.fontp->maxchar;
3096 currinf.set_char_p = currinf.fontp->set_char_p;
3097 #if FREETYPE
3098 if (currinf.set_char_p == set_ft_char)
3099 do_load_freetype_font();
3100 #endif
3101 }
3102
3103 if (currinf.set_char_p == set_char) {
3104 struct glyph *g;
3105 long x, y;
3106
3107 if (ch > maxchar)
3108 return 0; /* catch the error later */
3109 if ((g = &currinf.fontp->glyph[ch])->bitmap.bits == NULL) {
3110 if (g->addr == 0)
3111 return 0; /* catch the error later */
3112 if (g->addr == -1)
3113 return 0; /* previously flagged missing char */
3114 #if FREETYPE
3115 if (currinf.fontp->ft == NULL) /* if not freetype font */
3116 #endif
3117 {
3118 open_font_file(currinf.fontp);
3119 fseek(currinf.fontp->file, g->addr, SEEK_SET);
3120 }
3121 (*currinf.fontp->read_char) (currinf.fontp, ch);
3122 if (globals.debug & DBG_BITMAP)
3123 print_char((ubyte) ch, g);
3124 currinf.fontp->timestamp = ++current_timestamp;
3125 }
3126 #ifdef TEXXET
3127 if (geom_scan_frame == NULL) {
3128 long dvi_h_sav = DVI_H;
3129 if (currinf.dir < 0)
3130 DVI_H -= g->dvi_adv;
3131 #endif
3132 x = G_PXL_H - g->x;
3133 y = PXL_V - g->y;
3134 g_info->geom_box(info, x, y,
3135 x + g->bitmap.w - 1, y + g->bitmap.h - 1);
3136
3137 #ifdef TEXXET
3138 DVI_H = dvi_h_sav;
3139 }
3140 #endif
3141 return DIR * g->dvi_adv;
3142 }
3143 else if (currinf.set_char_p == set_vf_char) {
3144 struct macro *m;
3145 struct drawinf oldinfo;
3146 wide_ubyte oldmaxchar;
3147 #ifdef TEXXET
3148 long dvi_h_sav;
3149 #endif
3150
3151 if (ch > maxchar)
3152 return 0; /* catch the error later */
3153 if ((m = &currinf.fontp->macro[ch])->pos == NULL)
3154 return 0; /* catch the error later */
3155 #ifdef TEXXET
3156 dvi_h_sav = DVI_H;
3157 if (currinf.dir < 0)
3158 DVI_H -= m->dvi_adv;
3159 if (geom_scan_frame == NULL) {
3160 #endif
3161 oldinfo = currinf;
3162 oldmaxchar = maxchar;
3163 WW = XX = YY = ZZ = 0;
3164 currinf.tn_table_len = VFTABLELEN;
3165 currinf.tn_table = currinf.fontp->vf_table;
3166 currinf.tn_head = currinf.fontp->vf_chain;
3167 currinf.pos = m->pos;
3168 currinf.end = m->end;
3169 currinf.virtual = currinf.fontp;
3170 geom_scan_part(geom_do_char, fp, info, geom_current_frame, currinf.fontp->dimconv);
3171 currinf = oldinfo;
3172 maxchar = oldmaxchar;
3173 #ifdef TEXXET
3174 DVI_H = dvi_h_sav;
3175 }
3176 #endif
3177 return DIR * m->dvi_adv;
3178 }
3179 else {
3180 XDVI_FATAL((stderr, "currinf.set_char_p is not a registered routine!"));
3181 }
3182 /* NOTREACHED */
3183 return 0;
3184 }
3185
3186 /*
3187 * Do a rule in the geometry-scanning routine.
3188 */
3189
3190 static void
3191 geom_do_rule(struct scan_info *info, long h, long w)
3192 {
3193 long x, y;
3194 #ifdef TEXXET
3195 long dvi_h_save = DVI_H;
3196 #endif
3197 struct geom_info *g_info = info->data;
3198
3199 #ifdef TEXXET
3200 if (currinf.dir < 0)
3201 DVI_H -= w - 1;
3202 #endif
3203 x = G_PXL_H;
3204 y = PXL_V;
3205 g_info->geom_box(info, x, y - xpixel_round(h) + 1,
3206 x + xpixel_round(w) - 1, y);
3207 #ifdef TEXXET
3208 DVI_H = dvi_h_save;
3209 #endif
3210 }
3211
3212 /*
3213 * Geometric dvi scanner work routine. This does most of the work
3214 * (a) reading from a page, and (b) executing vf macros.
3215 */
3216
3217 void
3218 geom_scan_part(long(*char_proc)(FILE *, struct scan_info *, wide_ubyte),
3219 FILE *fp, struct scan_info *info, struct frame *minframe, double current_dimconv)
3220 {
3221 ubyte ch;
3222 #ifdef TEXXET
3223 struct drawinf oldinfo;
3224 wide_ubyte oldmaxchar = 0;
3225 off_t file_pos = 0;
3226 int refl_count = 0;
3227 #endif
3228
3229 currinf.fontp = NULL;
3230 currinf.set_char_p = set_no_char;
3231 #ifdef TEXXET
3232 currinf.dir = 1;
3233 geom_scan_frame = NULL; /* indicates we're not scanning */
3234 #endif
3235 for (;;) {
3236 ch = xone(fp);
3237 if (ch <= (ubyte)(SETCHAR0 + 127))
3238 DVI_H += char_proc(fp, info, ch);
3239 else if (FNTNUM0 <= ch && ch <= (ubyte) (FNTNUM0 + 63)) {
3240 change_font((unsigned long)(ch - FNTNUM0));
3241 }
3242 else {
3243 long a, b;
3244
3245 switch (ch) {
3246 case SET1:
3247 case PUT1:
3248 a = char_proc(fp, info, xone(fp));
3249 if (ch != PUT1)
3250 DVI_H += a;
3251 break;
3252
3253 case SET2:
3254 case PUT2:
3255 if (!resource.omega)
3256 dvi_fmt_error("%s:%d: draw_part: op-code %d only works with the \"-omega\" option",
3257 __FILE__, __LINE__, ch);
3258 else {
3259 #ifdef TEXXET
3260 char_proc(fp, info, xnum(fp, 2));
3261 #else
3262 a = char_proc(fp, info, xnum(fp, 2));
3263 if (ch != PUT2)
3264 DVI_H += a;
3265 #endif
3266 }
3267 break;
3268
3269 case SETRULE:
3270 /* Be careful, dvicopy outputs rules with
3271 height = 0x80000000. We don't want any
3272 SIGFPE here. */
3273 a = xsfour(fp);
3274 b = xspell_conv(xsfour(fp));
3275 if (a >= 0 && b >= 0
3276 #ifdef TEXXET
3277 && geom_scan_frame == NULL
3278 #endif
3279 ) {
3280 /* is this a geom scan? */
3281 if (info->geom_special != NULL)
3282 geom_do_rule(info, xspell_conv(a), b);
3283 }
3284 DVI_H += DIR * b;
3285 break;
3286
3287 case PUTRULE:
3288 a = xspell_conv(xsfour(fp));
3289 b = xspell_conv(xsfour(fp));
3290 if (a >= 0 && b >= 0
3291 #ifdef TEXXET
3292 && geom_scan_frame == NULL
3293 #endif
3294 ) {
3295 /* is this a geom scan? */
3296 if (info->geom_special != NULL)
3297 geom_do_rule(info, a, b);
3298 }
3299 break;
3300
3301 case NOP:
3302 break;
3303
3304 case BOP:
3305 xskip(fp, (long)11 * 4);
3306 DVI_H = G_OFFSET_X;
3307 DVI_V = G_OFFSET_Y;
3308 PXL_V = xpixel_conv(DVI_V);
3309 WW = XX = YY = ZZ = 0;
3310 /* create pagebreak in character scan */
3311 if (info->geom_special == NULL) {
3312 do_newpage(info);
3313 }
3314 break;
3315
3316 case PUSH:
3317 if (geom_current_frame->next == NULL) {
3318 struct frame *newp = xmalloc(sizeof *newp);
3319
3320 geom_current_frame->next = newp;
3321 newp->prev = geom_current_frame;
3322 newp->next = NULL;
3323 }
3324 geom_current_frame = geom_current_frame->next;
3325 geom_current_frame->data = currinf.data;
3326 break;
3327
3328 case POP:
3329 if (geom_current_frame == minframe)
3330 dvi_fmt_error("more POPs than PUSHes");
3331 currinf.data = geom_current_frame->data;
3332 geom_current_frame = geom_current_frame->prev;
3333 break;
3334
3335 #ifdef TEXXET
3336 case SREFL:
3337 if (geom_scan_frame == NULL) {
3338 /* we're not scanning: save some info. */
3339 oldinfo = currinf;
3340 oldmaxchar = maxchar;
3341 if (!currinf.virtual)
3342 file_pos = xtell(fp, currinf.pos);
3343 geom_scan_frame = geom_current_frame; /* start scanning */
3344 refl_count = 0;
3345 break;
3346 }
3347 /* we are scanning */
3348 if (geom_current_frame == geom_scan_frame)
3349 ++refl_count;
3350 break;
3351
3352 case EREFL:
3353 if (geom_scan_frame != NULL) { /* if we're scanning */
3354 if (geom_current_frame == geom_scan_frame
3355 && --refl_count < 0) {
3356 /* we've hit the end of our scan */
3357 geom_scan_frame = NULL;
3358 /* first: push */
3359 if (geom_current_frame->next == NULL) {
3360 struct frame *newp = xmalloc(sizeof *newp);
3361
3362 geom_current_frame->next = newp;
3363 newp->prev = geom_current_frame;
3364 newp->next = NULL;
3365 }
3366 geom_current_frame = geom_current_frame->next;
3367 geom_current_frame->data = currinf.data;
3368 /* next: restore old file position, XX, etc. */
3369 if (!currinf.virtual) {
3370 off_t bgn_pos = xtell(fp, G_dvi_buf_ptr);
3371
3372 if (file_pos >= bgn_pos) {
3373 oldinfo.pos = dvi_buffer + (file_pos - bgn_pos);
3374 oldinfo.end = currinf.end;
3375 }
3376 else {
3377 (void)lseek(fileno(fp), file_pos,
3378 SEEK_SET);
3379 oldinfo.pos = oldinfo.end;
3380 }
3381 }
3382 currinf = oldinfo;
3383 maxchar = oldmaxchar;
3384 /* and then: recover position info. */
3385 DVI_H = geom_current_frame->data.dvi_h;
3386 DVI_V = geom_current_frame->data.dvi_v;
3387 PXL_V = geom_current_frame->data.pxl_v;
3388 /* and finally, reverse direction */
3389 currinf.dir = -currinf.dir;
3390 }
3391 break;
3392 }
3393 /* we're not scanning, */
3394 /* so just reverse direction and then pop */
3395 currinf.dir = -currinf.dir;
3396 currinf.data = geom_current_frame->data;
3397 geom_current_frame = geom_current_frame->prev;
3398 break;
3399 #endif /* TEXXET */
3400
3401 case RIGHT1:
3402 case RIGHT2:
3403 case RIGHT3:
3404 case RIGHT4:
3405 DVI_H += DIR * xspell_conv(xsnum(fp, ch - RIGHT1 + 1));
3406 break;
3407
3408 case W1:
3409 case W2:
3410 case W3:
3411 case W4:
3412 WW = xspell_conv(xsnum(fp, ch - W0));
3413 case W0:
3414 DVI_H += DIR * WW;
3415 break;
3416
3417 case X1:
3418 case X2:
3419 case X3:
3420 case X4:
3421 XX = xspell_conv(xsnum(fp, ch - X0));
3422 case X0:
3423 DVI_H += DIR * XX;
3424 break;
3425
3426 case DOWN1:
3427 case DOWN2:
3428 case DOWN3:
3429 case DOWN4:
3430 DVI_V += xspell_conv(xsnum(fp, ch - DOWN1 + 1));
3431 PXL_V = xpixel_conv(DVI_V);
3432 break;
3433
3434 case Y1:
3435 case Y2:
3436 case Y3:
3437 case Y4:
3438 YY = xspell_conv(xsnum(fp, ch - Y0));
3439 case Y0:
3440 DVI_V += YY;
3441 PXL_V = xpixel_conv(DVI_V);
3442 break;
3443
3444 case Z1:
3445 case Z2:
3446 case Z3:
3447 case Z4:
3448 ZZ = xspell_conv(xsnum(fp, ch - Z0));
3449 case Z0:
3450 DVI_V += ZZ;
3451 PXL_V = xpixel_conv(DVI_V);
3452 break;
3453
3454 case FNT1:
3455 case FNT2:
3456 case FNT3:
3457 case FNT4:
3458 change_font(xnum(fp, ch - FNT1 + 1));
3459 break;
3460
3461 case XXX1:
3462 case XXX2:
3463 case XXX3:
3464 case XXX4:
3465 a = xnum(fp, ch - XXX1 + 1);
3466 if (a > 0) {
3467 char *str = read_special(fp, a);
3468
3469 /* is this a geom scan? */
3470 if (info->geom_special != NULL) {
3471 /* process the bounding box */
3472 geom_do_special(info, str, current_dimconv);
3473 /* process the specials we're looking for */
3474 info->geom_special(info, str, a);
3475 }
3476 }
3477 break;
3478
3479 case FNTDEF1:
3480 case FNTDEF2:
3481 case FNTDEF3:
3482 case FNTDEF4:
3483 xskip(fp, (long)(12 + ch - FNTDEF1 + 1));
3484 a = (long)xone(fp);
3485 xskip(fp, a + (long)xone(fp));
3486 break;
3487
3488 #ifndef TEXXET
3489 case SREFL:
3490 case EREFL:
3491 #endif
3492 case PRE:
3493 case POST:
3494 case POSTPOST:
3495 case EOP:
3496 default:
3497 return;
3498
3499 } /* end switch */
3500 } /* end else (ch not a SETCHAR or FNTNUM) */
3501 } /* end for */
3502 }
3503
3504
3505 /*
3506 * Main scanning routine.
3507 */
3508
3509 void
3510 geom_scan(long(*char_proc)(FILE *, struct scan_info *, wide_ubyte),
3511 FILE *fp, struct scan_info *info, int pageno)
3512 {
3513 volatile off_t pos_save = 0;
3514 struct drawinf currinf_save;
3515 ubyte maxchar_save;
3516
3517 #if PS
3518 if (info->geom_special != NULL && scanned_page < current_page) {
3519 fprintf(stderr, "shouldn't happen: %d >= %d!\n", scanned_page, current_page);
3520 return; /* should not happen */
3521 }
3522 #endif
3523
3524 if (dvi_pointer_frame != NULL)
3525 pos_save = lseek(fileno(fp), 0L, SEEK_CUR) - (dvi_pointer_frame->end - dvi_pointer_frame->pos);
3526
3527 (void) lseek(fileno(fp), pageinfo_get_offset(pageno), SEEK_SET);
3528
3529 currinf_save = currinf;
3530 maxchar_save = maxchar;
3531
3532 memset((char *)&currinf.data, '\0', sizeof currinf.data);
3533 currinf.tn_table_len = TNTABLELEN;
3534 currinf.tn_table = tn_table;
3535 currinf.tn_head = tn_head;
3536 currinf.pos = currinf.end = dvi_buffer;
3537 currinf.virtual = NULL;
3538
3539 if (!setjmp(info->done_env)) {
3540 geom_scan_part(char_proc, fp, info, geom_current_frame = &geom_frame0, dimconv);
3541 }
3542
3543 maxchar = maxchar_save;
3544 currinf = currinf_save;
3545
3546 if (dvi_pointer_frame != NULL) {
3547 (void)lseek(fileno(fp), pos_save, SEEK_SET);
3548 dvi_pointer_frame->pos = dvi_pointer_frame->end = dvi_buffer;
3549 }
3550 }
3551
3552
3553 /*
3554 * Routines for source special lookup
3555 */
3556
3557 struct src_spec_data {
3558 long x, y; /* coordinates we're looking for */
3559 unsigned long distance; /* best distance so far */
3560 Boolean recent_in_best; /* most recent string == XXX->best */
3561 struct src_parsed_special best; /* best special so far */
3562 struct src_parsed_special recent; /* most recent special */
3563 };
3564
3565 void
3566 src_parse(const char *str, int str_len, struct src_parsed_special *parsed)
3567 {
3568 const char *p = str;
3569
3570 if (*p >= '0' && *p <= '9') {
3571 parsed->line = atoi(p);
3572 do {
3573 ++p;
3574 str_len--;
3575 }
3576 while (*p >= '0' && *p <= '9');
3577 }
3578
3579 parsed->col = 0;
3580 if (*p == ':') {
3581 ++p;
3582 str_len--;
3583 parsed->col = atoi(p);
3584 while (*p >= '0' && *p <= '9') {
3585 ++p;
3586 str_len--;
3587 }
3588 }
3589
3590 if (*p == ' ') {
3591 ++p;
3592 str_len--;
3593 }
3594
3595 if (*p != '\0') {
3596 size_t len = str_len + 1;
3597
3598 if (len > parsed->filename_len) {
3599 if (parsed->filename_len != 0)
3600 free(parsed->filename);
3601 parsed->filename_len = (len & -8) + 64; /* rounding? */
3602 parsed->filename = xmalloc(parsed->filename_len);
3603 }
3604 memcpy(parsed->filename, p, len);
3605 }
3606 }
3607
3608 static void
3609 src_spec_box(struct scan_info *info, long ulx, long uly, long lrx, long lry)
3610 {
3611 struct geom_info *g_info = (struct geom_info *)info->data;
3612 struct src_spec_data *data = g_info->geom_data;
3613 unsigned long distance;
3614
3615 distance = 0;
3616
3617 if (data->x < ulx)
3618 distance += (ulx - data->x) * (ulx - data->x);
3619 else if (data->x > lrx)
3620 distance += (data->x - lrx) * (data->x - lrx);
3621
3622 if (data->y < uly)
3623 distance += (uly - data->y) * (uly - data->y);
3624 else if (data->y > lry)
3625 distance += (data->y - lry) * (data->y - lry);
3626
3627 if (distance < data->distance) {
3628 data->distance = distance;
3629
3630 /* Copy it over */
3631 if (!data->recent_in_best) {
3632 data->best.line = data->recent.line;
3633 data->best.col = data->recent.col;
3634 if (data->recent.filename_len != 0) {
3635 if (data->best.filename_len < data->recent.filename_len) {
3636 if (data->best.filename_len != 0)
3637 free(data->best.filename);
3638 data->best.filename_len = data->recent.filename_len;
3639 data->best.filename = xmalloc(data->best.filename_len);
3640 }
3641 memcpy(data->best.filename, data->recent.filename,
3642 data->recent.filename_len);
3643 }
3644
3645 data->recent_in_best = True;
3646 }
3647
3648 /* Quit early if we've found our glyph. */
3649 if (distance == 0 && data->best.filename_len != 0) {
3650 longjmp(info->done_env, 1);
3651 }
3652 }
3653 }
3654
3655 static void
3656 src_spec_special(struct scan_info *info, const char *str, int str_len)
3657 {
3658 struct geom_info *g_info = (struct geom_info *)info->data;
3659 struct src_spec_data *data = g_info->geom_data;
3660
3661 if (memcmp(str, "src:", 4) != 0)
3662 return;
3663
3664 src_parse(str + 4, str_len - 4, &data->recent);
3665
3666 /*
3667 * If this is the first special on the page, we may already have
3668 * spotted the nearest box.
3669 */
3670
3671 if (data->best.filename_len == 0) {
3672 data->best.line = data->recent.line;
3673 data->best.col = data->recent.col;
3674 if (data->recent.filename_len != 0) {
3675 if (data->best.filename_len < data->recent.filename_len) {
3676 if (data->best.filename_len != 0)
3677 free(data->best.filename);
3678 data->best.filename_len = data->recent.filename_len;
3679 data->best.filename = xmalloc(data->best.filename_len);
3680 }
3681 memcpy(data->best.filename, data->recent.filename,
3682 data->recent.filename_len);
3683
3684 data->recent_in_best = True;
3685 }
3686
3687 if (data->distance == 0) {
3688 longjmp(info->done_env, 1);
3689 }
3690 }
3691 else
3692 data->recent_in_best = False;
3693 }
3694
3695 /*
3696 * Routines for reverse searches on other pages.
3697 */
3698
3699 static struct src_parsed_special found;
3700 /* static jmp_buf scan_env; */
3701
3702 static Boolean
3703 scan_first_src_spcl(char *str, int str_len, void *data)
3704 {
3705 UNUSED(data);
3706 if (memcmp(str, "src:", 4) != 0)
3707 return False;
3708
3709 src_parse(str + 4, str_len - 4, &found);
3710
3711 return True;
3712 }
3713
3714 static Boolean
3715 scan_last_src_spcl(char *str, int str_len, void *data)
3716 {
3717 UNUSED(data);
3718 if (memcmp(str, "src:", 4) != 0)
3719 return False;
3720
3721 src_parse(str + 4, str_len - 4, &found);
3722
3723 return True;
3724 }
3725
3726
3727 static void
3728 src_spawn_editor(const struct src_parsed_special *parsed)
3729 {
3730 char *expanded_filename;
3731 char **argv;
3732 const char *p;
3733 char *q;
3734 int i;
3735
3736 struct stat buf;
3737
3738 /* first, determine the editor if necessary */
3739 if (resource.editor == NULL || *resource.editor == '\0') {
3740 p = getenv("XEDITOR");
3741 if (p != NULL)
3742 resource.editor = xstrdup(p);
3743 else {
3744
3745 p = getenv("VISUAL");
3746 if (p == NULL) {
3747 p = getenv("EDITOR");
3748 if (p == NULL) {
3749 popup_message(globals.widgets.top_level,
3750 MSG_WARN,
3751 /* help text */
3752 "Use the \"-editor\" command-line opion, the X resource "
3753 "\"xdvi.editor\" or one of the following environment variables "
3754 "to select the editor for source specials: "
3755 "\"XEDITOR\", \"VISUAL\" or \"EDITOR\".\n"
3756 "See the xdvi man page for more information on source specials "
3757 "and the editor options.",
3758 /* message */
3759 "No custom editor set - using vi as default.");
3760 p = "vi";
3761 }
3762 }
3763 q = xmalloc(strlen(p) + 10);
3764 memcpy(q, "xterm -e ", 9);
3765 strcpy(q + 9, p);
3766 resource.editor = q;
3767 }
3768 }
3769
3770 /* now try to open the file; find_file allocates space for expanded_filename */
3771 if ((expanded_filename = find_file(parsed->filename, &buf, kpse_tex_format)) == NULL) {
3772 popup_message(globals.widgets.top_level,
3773 MSG_ERR, NULL,
3774 "File \"%s\" not found, couldn't jump to special\n\"%s:%d\"\n",
3775 parsed->filename, parsed->filename, parsed->line);
3776 }
3777 else {
3778 TRACE_SRC((stderr, "source file \"%s\" expanded to \"%s\"\n", parsed->filename, expanded_filename));
3779 if (buf.st_mtime > globals.dvi_file.time) {
3780 statusline_info(STATUS_FOREVER,
3781 "Warning: TeX file is newer than dvi file - "
3782 "source special information might be wrong.");
3783 }
3784
3785 /* this allocates argv */
3786 argv = src_format_arguments(get_separated_list(resource.editor, " \t", True),
3787 expanded_filename, parsed->line, parsed->col);
3788
3789 fork_process(argv[0], False, NULL, NULL, NULL, 0, argv);
3790
3791 free(expanded_filename);
3792 for (i = 0; argv[i] != NULL; i++)
3793 free(argv[i]);
3794 free(argv);
3795 }
3796 }
3797
3798 off_t
3799 save_file_status(FILE *fp, struct drawinf *currinf_save, ubyte *maxchar_save)
3800 {
3801 off_t pos_save = 0;
3802 if (dvi_pointer_frame != NULL)
3803 pos_save = lseek(fileno(fp), 0L, SEEK_CUR)
3804 - (dvi_pointer_frame->end - dvi_pointer_frame->pos);
3805
3806 *currinf_save = currinf;
3807 *maxchar_save = maxchar;
3808 return pos_save;
3809 }
3810
3811 void
3812 restore_file_status(FILE *fp, struct drawinf currinf_save, ubyte maxchar_save, off_t pos_save)
3813 {
3814 maxchar = maxchar_save;
3815 currinf = currinf_save;
3816
3817 if (dvi_pointer_frame != NULL) {
3818 (void)lseek(fileno(fp), pos_save, SEEK_SET);
3819 dvi_pointer_frame->pos = dvi_pointer_frame->end = dvi_buffer;
3820 }
3821 }
3822
3823
3824
3825 /*
3826 * The main routine for source specials (reverse search).
3827 */
3828 void
3829 source_reverse_search(int x, int y, wide_bool call_editor)
3830 {
3831 struct scan_info info;
3832 struct geom_info g_info;
3833 struct src_spec_data data;
3834 struct src_parsed_special *foundp;
3835
3836 info.geom_special = src_spec_special;
3837
3838 g_info.geom_box = src_spec_box;
3839 g_info.geom_data = &data;
3840
3841 info.data = &g_info;
3842
3843 data.x = x;
3844 data.y = y;
3845 data.distance = 0xffffffff;
3846 data.recent_in_best = True;
3847 data.best.filename_len = data.recent.filename_len = 0;
3848 foundp = &data.best;
3849
3850 geom_scan(geom_do_char, globals.dvi_file.bak_fp, &info, current_page);
3851
3852 if (data.best.filename_len == 0) {
3853 /*
3854 * nothing found on current page;
3855 * scan next and previous pages with increasing offset
3856 */
3857 volatile int upper, lower;
3858 volatile off_t pos_save;
3859 struct drawinf currinf_save;
3860 ubyte maxchar_save;
3861
3862 /* Save file position */
3863 pos_save = save_file_status(globals.dvi_file.bak_fp, &currinf_save, &maxchar_save);
3864
3865 upper = lower = current_page;
3866 found.filename_len = 0; /* mark it as empty */
3867 for (;;) {
3868 if (++upper < total_pages) {
3869 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(upper), SEEK_SET);
3870 memset((char *)&currinf.data, '\0', sizeof currinf.data);
3871 currinf.tn_table_len = TNTABLELEN;
3872 currinf.tn_table = tn_table;
3873 currinf.tn_head = tn_head;
3874 currinf.pos = currinf.end = dvi_buffer;
3875 currinf.virtual = NULL;
3876
3877 if (spcl_scan(scan_first_src_spcl, NULL, True, globals.dvi_file.bak_fp)) {
3878 lower = upper;
3879 break;
3880 }
3881 }
3882 else if (lower < 0)
3883 break;
3884
3885 if (--lower >= 0) {
3886 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(lower), SEEK_SET);
3887
3888 memset((char *)&currinf.data, '\0', sizeof currinf.data);
3889 currinf.tn_table_len = TNTABLELEN;
3890 currinf.tn_table = tn_table;
3891 currinf.tn_head = tn_head;
3892 currinf.pos = currinf.end = dvi_buffer;
3893 currinf.virtual = NULL;
3894
3895 (void)spcl_scan(scan_last_src_spcl, NULL, False, globals.dvi_file.bak_fp);
3896 if (found.filename_len != 0)
3897 break;
3898 }
3899 }
3900
3901 if (found.filename_len != 0)
3902 statusline_info(STATUS_MEDIUM,
3903 "No source specials on this page - nearest on page %d",
3904 lower + globals.pageno_correct);
3905 else {
3906 /* nothing found at all; complain */
3907 xdvi_bell();
3908 popup_message(globals.widgets.top_level,
3909 MSG_ERR,
3910 /* helptext */
3911 reverse_search_helptext,
3912 /* popup */
3913 "No source specials in this DVI file - couldn't do reverse search.");
3914 }
3915
3916 /* Restore file status. */
3917 restore_file_status(globals.dvi_file.bak_fp, currinf_save, maxchar_save, pos_save);
3918
3919 foundp = &found;
3920 }
3921
3922 if (data.recent.filename_len != 0)
3923 free(data.recent.filename);
3924
3925 if (foundp->filename_len != 0) {
3926 if (call_editor) {
3927 src_spawn_editor(foundp);
3928 }
3929 else {
3930 statusline_info(STATUS_MEDIUM, "nearest special at (%d,%d): \"%s:%d\"",
3931 x / currwin.shrinkfactor, y / currwin.shrinkfactor, foundp->filename, foundp->line);
3932 }
3933 free(foundp->filename);
3934 }
3935 }
3936
3937
3938 /*
3939 * Debug routines for source special display (highlight the first box
3940 * after each source special, or highlight each box).
3941 */
3942
3943 struct src_spec_show_data {
3944 Boolean do_this_one; /* flag set after source special */
3945 Boolean do_them_all; /* flag to reset the above to */
3946 };
3947
3948 static void
3949 src_spec_show_box(struct scan_info *info, long ulx, long uly, long lrx, long lry)
3950 {
3951 struct geom_info *g_info = (struct geom_info *)info->data;
3952 struct src_spec_show_data *data = g_info->geom_data;
3953
3954 if (data->do_this_one) {
3955 long x = ulx / mane.shrinkfactor;
3956 long y = uly / mane.shrinkfactor;
3957
3958 XDrawRectangle(DISP, mane.win, globals.gc.high,
3959 x - mane_base_x, y - mane_base_y,
3960 lrx / mane.shrinkfactor - x,
3961 lry / mane.shrinkfactor - y);
3962 data->do_this_one = data->do_them_all;
3963 }
3964 }
3965
3966 static void
3967 src_spec_show_special(struct scan_info *info, const char *str, int str_len)
3968 {
3969 /* if (memcmp(str, "src:", 4) != 0) */
3970 /* return; */
3971 struct geom_info *g_info = (struct geom_info *)info->data;
3972 UNUSED(str_len);
3973 XDVI_INFO((stdout, "special: %s", str));
3974 ((struct src_spec_show_data *)g_info->geom_data)->do_this_one = True;
3975 }
3976
3977 void
3978 source_special_show(wide_bool do_them_all)
3979 {
3980 struct scan_info info;
3981 struct geom_info g_info;
3982 struct src_spec_show_data src_data;
3983
3984 info.geom_special = src_spec_show_special;
3985
3986 g_info.geom_box = src_spec_show_box;
3987 g_info.geom_data = &src_data;
3988
3989 info.data = &g_info;
3990
3991 src_data.do_this_one = src_data.do_them_all = do_them_all;
3992
3993 geom_scan(geom_do_char, globals.dvi_file.bak_fp, &info, current_page);
3994 }
3995
3996
3997 /*
3998 * Routines for forward search (look up a source line).
3999 *
4000 * The general procedure is:
4001 * (1) Use spcl_scan() to find the page containing the line (or at
4002 * least the closest line). This could be optimized further.
4003 * (2) Use geom_scan_part() to find the exact location of the source
4004 * special, and the box to highlight.
4005 */
4006
4007 /* These variables are referenced by src_scan_special(). */
4008
4009 static int src_this_line;
4010 static Boolean src_this_file_equal;
4011 static int src_line;
4012 static int src_col;
4013 static const char *src_file;
4014 static int src_page;
4015 /* static jmp_buf src_env; */
4016 static Boolean found_src;
4017 static unsigned long best_distance;
4018 static unsigned long best_col_dist;
4019 static int best_line;
4020 static int best_page;
4021 static off_t best_offset;
4022 static off_t max_offset;
4023
4024 /* Some of the above, plus these below, are used by geom_scan_part(). */
4025
4026 static Boolean src_fwd_active;
4027
4028 #define BBOX_INFO_MAXIDX 8 /* maximum number of bounding boxes */
4029
4030 /* list of bounding boxes */
4031 struct bbox_info {
4032 long min_x, max_x, min_y, max_y;
4033 long spcl_min_x, spcl_max_x, spcl_min_y, spcl_max_y;
4034 } g_bbox_info[BBOX_INFO_MAXIDX];
4035
4036 /* current index in g_bbox_info[] */
4037 static size_t bbox_info_idx = 0;
4038
4039 static Boolean
4040 src_scan_special(char *str, int str_len, void *data)
4041 {
4042 char *p;
4043 int col = 0;
4044 unsigned long distance;
4045 unsigned long col_dist;
4046
4047 UNUSED(data);
4048 if (memcmp(str, "src:", 4) != 0)
4049 return False;
4050
4051 found_src = True;
4052
4053 p = str + 4;
4054 str_len -= 4;
4055
4056 if (*p >= '0' && *p <= '9') {
4057 src_this_line = atoi(p);
4058 do {
4059 ++p;
4060 str_len--;
4061 }
4062 while (*p >= '0' && *p <= '9');
4063 }
4064
4065 if (*p == ':') {
4066 ++p;
4067 str_len--;
4068 col = atoi(p);
4069 while (*p >= '0' && *p <= '9') {
4070 ++p;
4071 str_len--;
4072 }
4073 }
4074
4075 if (*p == ' ') {
4076 ++p;
4077 str_len--;
4078 }
4079
4080 /* src specials might omit the file name when it had already
4081 been specified on the page; so skip the test when the
4082 special ends right here:
4083 */
4084 if (*p != '\0') {
4085 ASSERT(globals.dvi_file.dirname != NULL, "DVI name should be set here");
4086 src_this_file_equal = src_compare(p, str_len, src_file, globals.dvi_file.dirname, globals.dvi_file.dirlen);
4087 }
4088
4089 if (!src_this_file_equal)
4090 return False;
4091
4092 distance = (src_line > src_this_line
4093 ? src_line - src_this_line
4094 : 2 * (src_this_line - src_line)); /* favor earlier lines */
4095
4096 if (distance < best_distance) { /* found a better line */
4097 best_distance = distance;
4098 best_line = src_this_line;
4099 best_page = src_page;
4100 max_offset = best_offset = xtell(globals.dvi_file.bak_fp, currinf.pos);
4101 }
4102 else if (distance == best_distance) { /* still on a good line, remember diff */
4103 max_offset = xtell(globals.dvi_file.bak_fp, currinf.pos);
4104 }
4105
4106 if (distance == 0 && best_distance == 0) { /* found a better col */
4107 col_dist = (src_col > col ? src_col - col : 2 * (col - src_col));
4108
4109 if (col_dist < best_col_dist) {
4110 best_col_dist = col_dist;
4111 best_page = src_page;
4112 max_offset = best_offset = xtell(globals.dvi_file.bak_fp, currinf.pos);
4113 }
4114 else if (col_dist == best_col_dist) {
4115 max_offset = xtell(globals.dvi_file.bak_fp, currinf.pos);
4116 }
4117 }
4118 return True;
4119 }
4120
4121 static Boolean
4122 htex_scan_special(char *str, int str_len, void *data)
4123 {
4124 UNUSED(data);
4125
4126 if (g_anchor_pos == NULL)
4127 return False;
4128
4129 /* first, hypertex specials */
4130 if (memicmp(str, "html:", strlen("html:")) == 0) {
4131 size_t offset = strlen("html:");
4132 str += offset;
4133 str_len -= offset;
4134
4135 while (*str == ' ' || *str == '\t') {
4136 str++;
4137 str_len--;
4138 }
4139
4140 if (memicmp(str, "<a name", strlen("<a name")) != 0)
4141 return False;
4142
4143 offset = strlen("<a name");
4144 str += offset;
4145 str_len -= offset;
4146 str_len--; /* for the closing > */
4147
4148 /* skip over spaces, = and open quotes */
4149 while (*str == ' ' || *str == '=') {
4150 str++;
4151 str_len--;
4152 }
4153 if (*str == '"') {
4154 str++;
4155 str_len -= 2; /* don't compare closing quote */
4156 }
4157
4158 /* fprintf(stderr, "comparing: |%s|%s|%d\n", str, g_anchor_pos, MAX((size_t)str_len, g_anchor_len)); */
4159 if (memcmp(str, g_anchor_pos, MAX((size_t)str_len, g_anchor_len)) == 0)
4160 return True;
4161 else
4162 return False;
4163 }
4164 /* then, hdvips specials */
4165 else if (memicmp(str, "ps:", 3) == 0 && g_anchor_pos != NULL) {
4166 str += 3;
4167 if (memicmp(str, "sdict begin ", strlen("sdict begin ")) == 0) {
4168 char *ptr = NULL, *pptr = NULL;
4169 str += strlen("sdict begin ");
4170 if ((ptr = strstr(str, "/View")) != NULL
4171 && (ptr = strchr(ptr, '(')) != NULL
4172 && (pptr = strchr(ptr + 1, ')')) != NULL) {
4173 if (memcmp(ptr + 1, g_anchor_pos, pptr - ptr - 1) == 0
4174 && g_anchor_pos[pptr - ptr - 1] == '\0') {
4175 return True;
4176 }
4177 }
4178 }
4179 return False;
4180 }
4181 else {
4182 return False;
4183 }
4184 }
4185
4186 static void
4187 htex_scan_special_noreturn(struct scan_info *info, const char *str, int str_len)
4188 {
4189 struct geom_info *g_info = (struct geom_info *)info->data;
4190
4191 /* first, hypertex specials */
4192 if (memcmp(str, "html:", strlen("html:")) == 0) {
4193 size_t offset = strlen("html:");
4194 str += offset;
4195 str_len -= offset;
4196
4197 while (*str == ' ' || *str == '\t') {
4198 str++;
4199 str_len--;
4200 }
4201
4202 if (memicmp(str, "<a name", strlen("<a name")) != 0)
4203 return;
4204
4205 offset = strlen("<a name");
4206 str += offset;
4207 str_len -= offset;
4208 str_len--; /* for the closing > */
4209
4210 /* skip over spaces, = and open quotes */
4211 while (*str == ' ' || *str == '=') {
4212 str++;
4213 str_len--;
4214 }
4215 if (*str == '"') {
4216 str++;
4217 str_len -= 2; /* don't compare closing quote */
4218 }
4219
4220 if (g_anchor_pos != NULL
4221 && memcmp(str, g_anchor_pos, MAX((size_t)str_len, g_anchor_len)) == 0) {
4222 *((int *)g_info->geom_data) = pixel_conv(DVI_V);
4223 longjmp(info->done_env, 1);
4224 }
4225 }
4226 /* then, hdvips specials */
4227 else if (memicmp(str, "ps:", 3) == 0 && g_anchor_pos != NULL) {
4228 str += 3;
4229 if (memicmp(str, "sdict begin ", strlen("sdict begin ")) == 0) {
4230 char *ptr = NULL, *pptr = NULL;
4231 str += strlen("sdict begin ");
4232 if ((ptr = strstr(str, "/View")) != NULL
4233 && (ptr = strchr(ptr, '(')) != NULL
4234 && (pptr = strchr(ptr + 1, ')')) != NULL) {
4235 if (memcmp(ptr + 1, g_anchor_pos, pptr - ptr - 1) == 0
4236 && g_anchor_pos[pptr - ptr - 1] == '\0') {
4237 *((int *)g_info->geom_data) = pixel_conv(DVI_V);
4238 longjmp(info->done_env, 1);
4239 }
4240 }
4241 }
4242 }
4243 }
4244
4245 static void
4246 src_spec_fwd_box(struct scan_info *info, long ulx, long uly, long lrx, long lry)
4247 {
4248 /* Heuristic value to detect column breaks: A negative vertical
4249 offset to the next glyph larger than BBOX_OFF will create a new
4250 bounding box. The amount is in unshrunken pixels, i.e. 120
4251 \approx 1 Line of 12pt Text. */
4252 static const int BBOX_OFF = 360;
4253
4254 UNUSED(info);
4255 if (src_fwd_active) {
4256 /* probably a column break, create new box */
4257 if (lry < g_bbox_info[bbox_info_idx].max_y - BBOX_OFF
4258 && lrx > g_bbox_info[bbox_info_idx].max_x + 50) {
4259 if (bbox_info_idx < BBOX_INFO_MAXIDX - 1) {
4260 bbox_info_idx++;
4261
4262 g_bbox_info[bbox_info_idx].min_y = g_bbox_info[bbox_info_idx].min_x = LONG_MAX;
4263 g_bbox_info[bbox_info_idx].max_x = g_bbox_info[bbox_info_idx].max_y = 0;
4264 }
4265 }
4266 if (ulx < g_bbox_info[bbox_info_idx].min_x) {
4267 g_bbox_info[bbox_info_idx].min_x = ulx;
4268 }
4269 if (uly < g_bbox_info[bbox_info_idx].min_y) {
4270 g_bbox_info[bbox_info_idx].min_y = uly;
4271 }
4272 if (lrx > g_bbox_info[bbox_info_idx].max_x)
4273 g_bbox_info[bbox_info_idx].max_x = lrx;
4274 if (lry > g_bbox_info[bbox_info_idx].max_y) {
4275 g_bbox_info[bbox_info_idx].max_y = lry;
4276 }
4277 }
4278 }
4279
4280 /* dummy procedure for hyperlinks; we don't need geometry info for these. */
4281 static void
4282 htex_dummy_box(struct scan_info *info, long ulx, long uly, long lrx, long lry)
4283 {
4284 UNUSED(info);
4285 UNUSED(ulx);
4286 UNUSED(uly);
4287 UNUSED(lrx);
4288 UNUSED(lry);
4289 return;
4290 }
4291
4292 static void
4293 src_spec_fwd_special(struct scan_info *info, const char *str, int str_len)
4294 {
4295 long pos;
4296 UNUSED(str_len);
4297
4298 if (memcmp(str, "src:", 4) != 0) /* if no source special */
4299 return;
4300
4301 pos = xtell(globals.dvi_file.bak_fp, currinf.pos);
4302 if (pos >= best_offset)
4303 src_fwd_active = True;
4304
4305 if (src_fwd_active) {
4306 if (pos > max_offset)
4307 longjmp(info->done_env, 1);
4308
4309 if (G_PXL_H < g_bbox_info[bbox_info_idx].spcl_min_x)
4310 g_bbox_info[bbox_info_idx].spcl_min_x = G_PXL_H;
4311 if (G_PXL_H > g_bbox_info[bbox_info_idx].spcl_max_x)
4312 g_bbox_info[bbox_info_idx].spcl_max_x = G_PXL_H;
4313 if (PXL_V < g_bbox_info[bbox_info_idx].spcl_min_y)
4314 g_bbox_info[bbox_info_idx].spcl_min_y = PXL_V;
4315 if (PXL_V > g_bbox_info[bbox_info_idx].spcl_max_y)
4316 g_bbox_info[bbox_info_idx].spcl_max_y = PXL_V;
4317
4318 }
4319 }
4320
4321 /*
4322 * Routine to actually draw the box.
4323 */
4324
4325 static void
4326 source_fwd_draw_box(void)
4327 {
4328 if (globals.src.fwd_box_page != current_page) {
4329 globals.src.fwd_box_page = -1; /* different page---clear it */
4330 }
4331 else {
4332 size_t i;
4333 static const int padding = 15;
4334
4335 /* fix oversplitting by heuristics, by merging box n with box n+1 if they overlap */
4336 for (i = 0; i < bbox_info_idx; i++) {
4337 if (g_bbox_info[i].min_x == LONG_MAX)
4338 continue;
4339 if ((g_bbox_info[i+1].min_x < g_bbox_info[i].max_x
4340 && g_bbox_info[i+1].min_y < g_bbox_info[i].max_y) ||
4341 (g_bbox_info[i].min_x < g_bbox_info[i+1].max_x
4342 && g_bbox_info[i].min_y < g_bbox_info[i+1].max_y)) { /* overlap */
4343 g_bbox_info[i].min_x = MIN(g_bbox_info[i].min_x, g_bbox_info[i+1].min_x);
4344 g_bbox_info[i].min_y = MIN(g_bbox_info[i].min_y, g_bbox_info[i+1].min_y);
4345 g_bbox_info[i].max_x = MAX(g_bbox_info[i].max_x, g_bbox_info[i+1].max_x);
4346 g_bbox_info[i].max_y = MAX(g_bbox_info[i].max_y, g_bbox_info[i+1].max_y);
4347 bbox_info_idx--;
4348 }
4349 }
4350
4351 for (i = 0; i <= bbox_info_idx; i++) {
4352 int min_x, min_y, max_x, max_y;
4353
4354 volatile XPoint ul, ur, ll, lr;
4355
4356 if (g_bbox_info[i].min_x == LONG_MAX) {
4357 /* If no glyphs or rules, use hot point of special instead. */
4358 g_bbox_info[i].min_x = g_bbox_info[i].spcl_min_x;
4359 g_bbox_info[i].min_y = g_bbox_info[i].spcl_min_y;
4360 g_bbox_info[i].max_x = g_bbox_info[i].spcl_max_x;
4361 g_bbox_info[i].max_y = g_bbox_info[i].spcl_max_y;
4362 }
4363
4364 min_x = (g_bbox_info[i].min_x - padding) / mane.shrinkfactor - mane_base_x;
4365 min_y = (g_bbox_info[i].min_y - padding) / mane.shrinkfactor - mane_base_y;
4366 max_x = (g_bbox_info[i].max_x + padding) / mane.shrinkfactor - mane_base_x;
4367 max_y = (g_bbox_info[i].max_y + padding) / mane.shrinkfactor - mane_base_y;
4368
4369 ul.x = min_x;
4370 ul.y = min_y;
4371
4372 ur.x = max_x;
4373 ur.y = min_y;
4374
4375 ll.x = min_x;
4376 ll.y = max_y;
4377
4378 lr.x = max_x;
4379 lr.y = max_y;
4380
4381 if (i == 0 && bbox_info_idx == 0) { /* only 1 bounding box */
4382 /* (2009-08-23) XDrawRectangle is broken in some X
4383 implementations (see sourceforge bug #2841005),
4384 so use XDrawLines instead
4385 XDrawRectangle(DISP, mane.win, globals.gc.high,
4386 min_x, min_y, max_x - min_x, max_y - min_y);
4387 */
4388 XPoint points[5];
4389 points[0] = ll;
4390 points[1] = ul;
4391 points[2] = ur;
4392 points[3] = lr;
4393 points[4] = ll;
4394 XDrawLines(DISP, mane.win, globals.gc.high, points, 5, CoordModeOrigin);
4395 }
4396 else if (i == 0) { /* draw first bbox with open bottom */
4397 XPoint points[4];
4398 points[0] = ll;
4399 points[1] = ul;
4400 points[2] = ur;
4401 points[3] = lr;
4402 XDrawLines(DISP, mane.win, globals.gc.high, points, 4, CoordModeOrigin);
4403 }
4404 else if (i > 0 && i < bbox_info_idx) { /* draw middle box with open top and open bottom */
4405 XPoint points[2];
4406 points[0] = ul;
4407 points[1] = ll;
4408 XDrawLines(DISP, mane.win, globals.gc.high, points, 2, CoordModeOrigin);
4409 points[0] = ur;
4410 points[1] = lr;
4411 XDrawLines(DISP, mane.win, globals.gc.high, points, 2, CoordModeOrigin);
4412 }
4413 else { /* draw last box with open top */
4414 XPoint points[4];
4415 points[0] = ul;
4416 points[1] = ll;
4417 points[2] = lr;
4418 points[3] = ur;
4419 XDrawLines(DISP, mane.win, globals.gc.high, points, 4, CoordModeOrigin);
4420 }
4421 }
4422 }
4423 }
4424
4425 #if 0
4426 #include<asm/msr.h>
4427 unsigned long time_start=0, time_end=0;
4428 #endif
4429
4430 void
4431 source_forward_search(const char *str)
4432 {
4433 volatile off_t pos_save = 0;
4434 struct drawinf currinf_save;
4435 ubyte maxchar_save;
4436 struct scan_info info;
4437 struct geom_info g_info;
4438
4439 TRACE_CLIENT((stderr, "Entering source_forward_search(%s)", str));
4440
4441 max_offset = 0;
4442 src_file = str;
4443 while (*src_file == '0')
4444 ++src_file;
4445 if (*src_file < '1' || *src_file > '9') {
4446 XDVI_WARNING((stderr, "Ignoring malformed source special \"%s\"", str));
4447 return;
4448 }
4449 src_line = atoi(src_file);
4450 while (*src_file >= '0' && *src_file <= '9')
4451 ++src_file;
4452
4453 src_col = 0;
4454 if (*src_file == ':') {
4455 ++src_file;
4456 src_col = atoi(src_file);
4457 while (*src_file >= '0' && *src_file <= '9')
4458 ++src_file;
4459 }
4460
4461 if (*src_file == ' ')
4462 ++src_file;
4463
4464 TRACE_CLIENT((stderr, "File = \"%s\", line = %d, col = %d", src_file, src_line, src_col));
4465
4466 /* Save status of dvi_file reading (in case we hit an error and resume
4467 drawing). */
4468
4469 if (dvi_pointer_frame != NULL)
4470 pos_save = lseek(fileno(globals.dvi_file.bak_fp), 0L, SEEK_CUR) - (dvi_pointer_frame->end - dvi_pointer_frame->pos);
4471 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(0), SEEK_SET);
4472
4473 currinf_save = currinf;
4474 maxchar_save = maxchar;
4475
4476 memset((char *)&currinf.data, '\0', sizeof currinf.data);
4477 currinf.tn_table_len = TNTABLELEN;
4478 currinf.tn_table = tn_table;
4479 currinf.tn_head = tn_head;
4480 currinf.pos = currinf.end = dvi_buffer;
4481 currinf.virtual = NULL;
4482
4483 /* Start search over pages */
4484 #if 0
4485 rdtscl(time_start);
4486 #endif
4487
4488 found_src = False;
4489 best_distance = best_col_dist = ULONG_MAX;
4490 src_this_line = 0; /* initialize things that are kept as defaults */
4491 src_this_file_equal = False;
4492
4493 /* These two lines do the actual scanning (first pass). */
4494 for (src_page = 0; src_page < total_pages; ++src_page)
4495 (void)spcl_scan(src_scan_special, NULL, False, globals.dvi_file.bak_fp);
4496
4497 if (best_distance == ULONG_MAX) {
4498 if (!found_src) {
4499 popup_message(globals.widgets.top_level,
4500 MSG_WARN,
4501 /* helptext */
4502 reverse_search_helptext,
4503 /* popup */
4504 "No source specials in this DVI file - couldn't do reverse search.");
4505 }
4506 else {
4507 popup_message(globals.widgets.top_level,
4508 MSG_WARN,
4509 /* helptext */
4510 "To enable reverse search, the TeX file has to be compiled with source specials. "
4511 "See the xdvi man page (section SOURCE SPECIALS) for details.",
4512 /* popup */
4513 "No references to source file \"%s\" in DVI file.",
4514 src_file);
4515 }
4516
4517 /* Restore file position. */
4518 maxchar = maxchar_save;
4519 currinf = currinf_save;
4520
4521 if (dvi_pointer_frame != NULL) {
4522 (void)lseek(fileno(globals.dvi_file.bak_fp), pos_save, SEEK_SET);
4523 dvi_pointer_frame->pos = dvi_pointer_frame->end = dvi_buffer;
4524 }
4525
4526 return;
4527 }
4528 TRACE_CLIENT((stderr, "Match: line %d on page %d, offset %lu",
4529 best_line, best_page + globals.pageno_correct, (unsigned long)best_offset));
4530
4531 /*
4532 * In this case we don't need to restore maxchar and currinf, since
4533 * we won't resume drawing -- we'll jump to a new page instead.
4534 */
4535
4536 /* Move to that page. */
4537 goto_page(best_page, resource.keep_flag ? NULL : home, False);
4538 page_history_insert(best_page);
4539 globals.ev.flags |= EV_NEWPAGE; /* so that existing mark gets erased */
4540 /* Now search that particular page. */
4541
4542 info.geom_special = src_spec_fwd_special;
4543
4544 g_info.geom_box = src_spec_fwd_box;
4545 g_info.geom_data = NULL;
4546
4547 src_fwd_active = False;
4548 bbox_info_idx = 0;
4549
4550 g_bbox_info[bbox_info_idx].min_x =
4551 g_bbox_info[bbox_info_idx].min_y =
4552 g_bbox_info[bbox_info_idx].spcl_min_x =
4553 g_bbox_info[bbox_info_idx].spcl_min_y = LONG_MAX;
4554 g_bbox_info[bbox_info_idx].max_x =
4555 g_bbox_info[bbox_info_idx].max_y =
4556 g_bbox_info[bbox_info_idx].spcl_max_x =
4557 g_bbox_info[bbox_info_idx].spcl_max_y = 0;
4558 globals.src.fwd_box_page = -1; /* in case of error, suppress box */
4559
4560 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(best_page), SEEK_SET);
4561 currinf.tn_table_len = TNTABLELEN;
4562 currinf.tn_table = tn_table;
4563 currinf.tn_head = tn_head;
4564 currinf.pos = currinf.end = dvi_buffer;
4565 currinf.virtual = NULL;
4566
4567 info.data = &g_info;
4568
4569 if (!setjmp(info.done_env))
4570 geom_scan_part(geom_do_char, globals.dvi_file.bak_fp, &info, geom_current_frame = &geom_frame0, dimconv);
4571
4572 if (!src_fwd_active) {
4573 XDVI_ERROR((stderr, "%s:%d: shouldn't happen: geom_scan_part() failed to re-find the special.", __FILE__, __LINE__));
4574 }
4575 else {
4576 long x_min = g_bbox_info[bbox_info_idx].min_x;
4577 long y_min = g_bbox_info[bbox_info_idx].min_y;
4578 long x_max = g_bbox_info[bbox_info_idx].max_x;
4579 long y_max = g_bbox_info[bbox_info_idx].max_y;
4580 do_autoscroll = True;
4581 globals.src.fwd_box_page = current_page;
4582 if (x_min == LONG_MAX || x_max == LONG_MAX) {
4583 /* If no glyphs or rules, use hot point of special instead. */
4584 x_min = g_bbox_info[bbox_info_idx].spcl_min_x;
4585 y_min = g_bbox_info[bbox_info_idx].spcl_min_y;
4586 x_max = g_bbox_info[bbox_info_idx].spcl_max_x;
4587 y_max = g_bbox_info[bbox_info_idx].spcl_max_y;
4588 }
4589 scroll_page_if_needed((int)(x_min / currwin.shrinkfactor) + 2, (int)(x_max / currwin.shrinkfactor) - 2,
4590 (int)(y_min / currwin.shrinkfactor) + 10, (int)(y_max / currwin.shrinkfactor) - 10);
4591 }
4592 #if 0
4593 rdtscl(time_end);
4594 printf("time search: %lu\n", time_end - time_start);
4595 #endif
4596 }
4597
4598
4599 void
4600 anchor_search(const char *str)
4601 {
4602 off_t pos_save = 0;
4603 struct drawinf currinf_save;
4604 ubyte maxchar_save;
4605 volatile int test_page = 0;
4606 Boolean found_anchor = False;
4607 int y_pos = -1;
4608 struct scan_info info;
4609 struct geom_info g_info;
4610
4611 ASSERT(str != NULL, "Argument to anchor_search() musn't be NULL");
4612 TRACE_HTEX((stderr, "Entering anchor_search(%s)", str));
4613
4614 /* Save status of dvi_file reading (in case we hit an error and resume drawing). */
4615 if (dvi_pointer_frame != NULL)
4616 pos_save = lseek(fileno(globals.dvi_file.bak_fp), 0L, SEEK_CUR) - (dvi_pointer_frame->end - dvi_pointer_frame->pos);
4617 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(0), SEEK_SET);
4618
4619 currinf_save = currinf;
4620 maxchar_save = maxchar;
4621
4622 memset((char *)&currinf.data, '\0', sizeof currinf.data);
4623 currinf.tn_table_len = TNTABLELEN;
4624 currinf.tn_table = tn_table;
4625 currinf.tn_head = tn_head;
4626 currinf.pos = currinf.end = dvi_buffer;
4627 currinf.virtual = NULL;
4628
4629 /* Start search over pages */
4630 for (test_page = 0; test_page < total_pages; test_page++) {
4631 if (spcl_scan(htex_scan_special, NULL, True, globals.dvi_file.bak_fp)) {
4632 TRACE_HTEX((stderr, "Found anchor on page %d", test_page));
4633 found_anchor = True;
4634 break;
4635 }
4636 }
4637
4638 if (!found_anchor) {
4639 TRACE_HTEX((stderr, "Anchor not found"));
4640 /* Restore file position. */
4641 maxchar = maxchar_save;
4642 currinf = currinf_save;
4643
4644 if (dvi_pointer_frame != NULL) {
4645 (void)lseek(fileno(globals.dvi_file.bak_fp), pos_save, SEEK_SET);
4646 dvi_pointer_frame->pos = dvi_pointer_frame->end = dvi_buffer;
4647 }
4648 xdvi_bell();
4649 statusline_error(STATUS_MEDIUM, "Error: Anchor \"%s\" not found.", str);
4650 return;
4651 }
4652
4653 /*
4654 * In this case we don't need to restore maxchar and currinf, since
4655 * we won't resume drawing -- we'll jump to a new page instead.
4656 */
4657
4658 /* Move to that page. */
4659 (void)lseek(fileno(globals.dvi_file.bak_fp), pageinfo_get_offset(test_page), SEEK_SET);
4660 currinf.tn_table_len = TNTABLELEN;
4661 currinf.tn_table = tn_table;
4662 currinf.tn_head = tn_head;
4663 currinf.pos = currinf.end = dvi_buffer;
4664 currinf.virtual = NULL;
4665
4666 info.geom_special = htex_scan_special_noreturn;
4667
4668 g_info.geom_box = htex_dummy_box;
4669 g_info.geom_data = &y_pos;
4670
4671 info.data = &g_info;
4672
4673 if (!setjmp(info.done_env))
4674 geom_scan_part(geom_do_char, globals.dvi_file.bak_fp, &info, geom_current_frame = &geom_frame0, dimconv);
4675
4676 if (y_pos == -1) { /* not found */
4677 XDVI_ERROR((stderr, "%s:%d: shouldn't happen: geom_scan_part() failed to re-find the link.", __FILE__, __LINE__));
4678 }
4679 else {
4680 goto_page(test_page, resource.keep_flag ? NULL : home, False);
4681 page_history_insert(test_page);
4682 do_autoscroll = True;
4683 TRACE_HTEX((stderr, "Found anchor on position %d", y_pos));
4684 htex_set_anchormarker(y_pos);
4685 }
4686 /* reset info */
4687 free(g_anchor_pos);
4688 g_anchor_pos = NULL;
4689 g_anchor_len = 0;
4690 }
4691
4692
4693 #if GREY
4694
4695 static void
4696 mask_shifts(Pixel mask, int *pshift1, int *pshift2)
4697 {
4698 int k, l;
4699
4700 for (k = 0; (mask & 1) == 0; ++k)
4701 mask >>= 1;
4702 for (l = 0; (mask & 1) == 1; ++l)
4703 mask >>= 1;
4704 *pshift1 = sizeof(short) * 8 - l;
4705 *pshift2 = k;
4706 }
4707
4708 /*
4709 * Try to allocate 4 color planes for 16 colors (for GXor drawing).
4710 * Not called if the visual type is TrueColor.
4711 * When color specials are active, this is called exactly once.
4712 * It is used for the first foreground/background pair displayed in
4713 * the document. For other documents, we act as if this call had failed.
4714 */
4715
4716
4717 void
4718 init_plane_masks(void)
4719 {
4720 Pixel pixel;
4721
4722 if (globals.gc.do_copy || plane_masks[0] != 0)
4723 return;
4724
4725 if (XAllocColorCells(DISP, G_colormap, False, plane_masks, 4, &pixel, 1)) {
4726 /* Make sure fore_Pixel and back_Pixel are a part of the palette */
4727 resource.back_Pixel = pixel;
4728 resource.fore_Pixel = pixel | plane_masks[0] | plane_masks[1]
4729 | plane_masks[2] | plane_masks[3];
4730 if (mane.win != (Window) 0)
4731 XSetWindowBackground(DISP, mane.win, resource.back_Pixel);
4732 }
4733 else {
4734 globals.gc.do_copy = True;
4735 warn_overstrike();
4736 }
4737 }
4738
4739 #endif /* GREY */
4740
4741 #if COLOR
4742
4743 /*
4744 * Insert into list of colors to be freed upon opening new document.
4745 */
4746
4747 static void
4748 color_list_insert(Pixel pixel)
4749 {
4750 if (color_list_len >= color_list_max) {
4751 if (color_list_max == 0)
4752 color_list = xmalloc((color_list_max += 16) * sizeof *color_list);
4753 else
4754 color_list = xrealloc(color_list,
4755 (color_list_max += 16) * sizeof *color_list);
4756 }
4757 color_list[color_list_len++] = pixel;
4758 }
4759
4760
4761 /*
4762 * Warn about colors not being correct.
4763 */
4764
4765 static void
4766 color_warn(void)
4767 {
4768 if (!color_warned) {
4769 color_warned = True;
4770 popup_message(globals.widgets.top_level,
4771 MSG_WARN,
4772 /* helptext */
4773 "Either this document is using too many "
4774 "colors, or some other application is. "
4775 "In the latter case, try to close that "
4776 "application and re-read the DVI file.",
4777 /* msg */
4778 "Cannot allocate colormap entry, "
4779 "displayed colors will not be exact.");
4780 }
4781 }
4782
4783
4784 /*
4785 * Allocate a color and add it to our list of pixels to be returned
4786 * upon opening a new document.
4787 */
4788
4789 #define SHIFTIFY(x, shift1, shift2) ((((Pixel)(x)) >> (shift1)) << (shift2))
4790
4791 static int shift1_r, shift1_g, shift1_b;
4792 static int shift2_r, shift2_g, shift2_b;
4793 static Boolean shifts_good = False;
4794
4795 Pixel
4796 alloc_color(const struct rgb *colorp, Pixel fallback_pixel)
4797 {
4798 XColor xcol;
4799
4800 if (G_visual->class == TrueColor) {
4801 if (!shifts_good) {
4802 mask_shifts(G_visual->red_mask, &shift1_r, &shift2_r);
4803 mask_shifts(G_visual->green_mask, &shift1_g, &shift2_g);
4804 mask_shifts(G_visual->blue_mask, &shift1_b, &shift2_b);
4805 shifts_good = True;
4806 }
4807 return SHIFTIFY(colorp->r, shift1_r, shift2_r)
4808 | SHIFTIFY(colorp->g, shift1_g, shift2_g)
4809 | SHIFTIFY(colorp->b, shift1_b, shift2_b);
4810 }
4811 else {
4812 xcol.red = colorp->r;
4813 xcol.green = colorp->g;
4814 xcol.blue = colorp->b;
4815 xcol.flags = DoRed | DoGreen | DoBlue;
4816 if (XAllocColor(DISP, G_colormap, &xcol)) {
4817 color_list_insert(xcol.pixel);
4818 if (globals.debug & DBG_DVI)
4819 printf("alloc_color%6d%6d%6d --> %ld\n",
4820 xcol.red, xcol.green, xcol.blue, xcol.pixel);
4821 return xcol.pixel;
4822 }
4823 else {
4824 if (globals.debug & DBG_DVI)
4825 printf("alloc_color%6d%6d%6d --> failed\n",
4826 xcol.red, xcol.green, xcol.blue);
4827 color_warn();
4828 return fallback_pixel;
4829 }
4830 }
4831 }
4832
4833 #undef SHIFTIFY
4834
4835
4836 /*
4837 * Switch colors of GCs, etc. Called when we're about to use a GC.
4838 */
4839
4840 void
4841 do_color_change(void)
4842 {
4843 static int shrink_allocated_for = 0;
4844 Pixel set_bits;
4845 Pixel clr_bits;
4846
4847 ASSERT(fg_current != NULL, "Current foreground mustn't be NULL");
4848 ASSERT(bg_current != NULL, "Current background mustn't be NULL");
4849 #if GREY
4850 if (resource.use_grey) {
4851 if (G_visual->class == TrueColor) {
4852 if (!fg_current->pixel_good) {
4853 fg_current->pixel = alloc_color(&fg_current->color,
4854 color_data[0].pixel);
4855 fg_current->pixel_good = True;
4856 }
4857
4858 set_bits = fg_current->pixel & ~bg_current->pixel;
4859 clr_bits = bg_current->pixel & ~fg_current->pixel;
4860
4861 if (set_bits & G_visual->red_mask)
4862 set_bits |= G_visual->red_mask;
4863 if (clr_bits & G_visual->red_mask)
4864 clr_bits |= G_visual->red_mask;
4865 if (set_bits & G_visual->green_mask)
4866 set_bits |= G_visual->green_mask;
4867 if (clr_bits & G_visual->green_mask)
4868 clr_bits |= G_visual->green_mask;
4869 if (set_bits & G_visual->blue_mask)
4870 set_bits |= G_visual->blue_mask;
4871 if (clr_bits & G_visual->blue_mask)
4872 clr_bits |= G_visual->blue_mask;
4873
4874 /*
4875 * Make the GCs
4876 */
4877
4878 globals.gc.rule = set_or_make_gc(globals.gc.rule, GXcopy, fg_current->pixel, bg_current->pixel);
4879 globals.gc.fore2 = NULL;
4880
4881 if (resource.copy
4882 || (set_bits && clr_bits && !resource.thorough)) {
4883 if (!resource.copy) {
4884 warn_overstrike();
4885 }
4886 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXcopy, fg_current->pixel, bg_current->pixel);
4887 }
4888 else {
4889 if (set_bits) {
4890 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXor, fg_current->pixel & set_bits, 0);
4891 if (clr_bits) {
4892 globals.gc.fore2 = globals.gc.fore2_bak = set_or_make_gc(globals.gc.fore2_bak, GXandInverted,
4893 ~fg_current->pixel & clr_bits, 0);
4894 }
4895 }
4896 else {
4897 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXandInverted,
4898 ~fg_current->pixel & clr_bits, 0);
4899 }
4900 }
4901
4902 if (globals.debug & DBG_DVI)
4903 printf("do_color_change: fg = %lx, bg = %lx, with%s globals.gc.fore2\n",
4904 fg_current->pixel, bg_current->pixel,
4905 globals.gc.fore2 == NULL ? "out" : "");
4906
4907 if (mane.shrinkfactor > 1) {
4908 unsigned int i;
4909 unsigned int sf_squared;
4910
4911 sf_squared = mane.shrinkfactor * mane.shrinkfactor;
4912
4913 if (shrink_allocated_for < mane.shrinkfactor) {
4914 if (pixeltbl != NULL) {
4915 free((char *) pixeltbl);
4916 if (pixeltbl_gc2 != NULL) {
4917 free((char *) pixeltbl_gc2);
4918 pixeltbl_gc2 = NULL;
4919 }
4920 }
4921 pixeltbl = xmalloc((sf_squared + 1) * sizeof *pixeltbl);
4922 shrink_allocated_for = mane.shrinkfactor;
4923 }
4924 if (globals.gc.fore2 != NULL && pixeltbl_gc2 == NULL) {
4925 /* Can't use sf_squared (or mane.shrinkfactor) here */
4926 pixeltbl_gc2 = xmalloc((shrink_allocated_for * shrink_allocated_for + 1) * sizeof *pixeltbl_gc2);
4927 }
4928
4929 /*
4930 * Initialize the pixel lookup table according to the gamma values.
4931 */
4932 #define SHIFTIFY(x, shift1, shift2) ((((Pixel)(x)) >> (shift1)) << (shift2))
4933
4934 for (i = 0; i <= sf_squared; ++i) {
4935 double frac = resource.gamma > 0
4936 ? pow((double)i / sf_squared, 1 / resource.gamma)
4937 : 1 - pow((double) (sf_squared - i) / sf_squared, -resource.gamma);
4938 unsigned int red, green, blue;
4939 Pixel pixel;
4940 /* fprintf(stderr, "frac: %f\n", frac); */
4941 /* fprintf(stderr, "fg_current: %d, bg_current: %d\n", fg_current->color.r, bg_current->color.r); */
4942 red = frac
4943 * ((double) fg_current->color.r - bg_current->color.r)
4944 + bg_current->color.r;
4945 green = frac
4946 * ((double) fg_current->color.g - bg_current->color.g)
4947 + bg_current->color.g;
4948 blue = frac
4949 * ((double) fg_current->color.b - bg_current->color.b)
4950 + bg_current->color.b;
4951
4952 pixel = SHIFTIFY(red, shift1_r, shift2_r)
4953 | SHIFTIFY(green, shift1_g, shift2_g)
4954 | SHIFTIFY(blue, shift1_b, shift2_b);
4955
4956 if (globals.gc.fore2 != NULL) { /* if thorough */
4957 /* fprintf(stderr, "++++++ pixeltable at %d: %ld\n", i, pixel & ~bg_current->pixel); */
4958 pixeltbl[i] = pixel & ~bg_current->pixel;
4959 pixeltbl_gc2[i] = ~pixel & bg_current->pixel;
4960 }
4961 else if (resource.copy || (set_bits && clr_bits)) {
4962 /* fprintf(stderr, "++++++ pixeltable2 at %d: %ld\n", i, pixel); */
4963 pixeltbl[i] = pixel;
4964 }
4965 else {
4966 /* fprintf(stderr, "++++++ pixeltable3 at %d: 0x%lx\n", i, ~pixel & clr_bits); */
4967 /* fprintf(stderr, "++++++ pixeltable3 at %d: %ld\n", i, pixel & set_bits); */
4968 pixeltbl[i] = set_bits ? pixel & set_bits : ~pixel & clr_bits;
4969 }
4970 }
4971 #undef SHIFTIFY
4972 }
4973
4974 }
4975 else { /* not TrueColor */
4976 int i;
4977 Boolean using_planes;
4978
4979 using_planes = (fg_current == bg_head->fg_head && !globals.gc.do_copy);
4980 if (!fg_current->palette_good) {
4981 XColor color;
4982
4983 /*
4984 * Initialize the pixel lookup table according to the gamma values.
4985 */
4986 for (i = 0; i < 16; ++i) {
4987 double frac;
4988
4989 frac = resource.gamma > 0
4990 ? pow((double) i / 15, 1 / resource.gamma)
4991 : 1 - pow((double) (15 - i) / 15, -resource.gamma);
4992 color.red = frac
4993 * ((double) fg_current->color.r - bg_current->color.r)
4994 + bg_current->color.r;
4995 color.green = frac
4996 * ((double) fg_current->color.g - bg_current->color.g)
4997 + bg_current->color.g;
4998 color.blue = frac
4999 * ((double) fg_current->color.b - bg_current->color.b)
5000 + bg_current->color.b;
5001
5002 color.flags = DoRed | DoGreen | DoBlue;
5003
5004 if (using_planes) {
5005 color.pixel = resource.back_Pixel; /* start of block */
5006 if (i & 1) color.pixel |= plane_masks[0];
5007 if (i & 2) color.pixel |= plane_masks[1];
5008 if (i & 4) color.pixel |= plane_masks[2];
5009 if (i & 8) color.pixel |= plane_masks[3];
5010 XStoreColor(DISP, G_colormap, &color);
5011 fg_current->palette[i] = color.pixel;
5012 }
5013 else {
5014 if (XAllocColor(DISP, G_colormap, &color)) {
5015 fg_current->palette[i] = color.pixel;
5016 color_list_insert(color.pixel);
5017 }
5018 else {
5019 color_warn();
5020 fg_current->palette[i] =
5021 (i * 100 >= resource.density * 15)
5022 ? resource.fore_Pixel : bg_current->pixel;
5023 }
5024 }
5025 }
5026
5027 if (using_planes && bg_current->pixel != resource.back_Pixel) {
5028 bg_current->pixel = resource.back_Pixel;
5029 /* XSetWindowBackground(DISP, mane.win, bg_current->pixel); */
5030 #if MOTIF
5031 fprintf(stderr, "setting window background!\n");
5032 XSetWindowBackground(DISP, XtWindow(globals.widgets.main_window), bg_current->pixel);
5033 XtVaSetValues(globals.widgets.main_window, XmNbackground, bg_current->pixel, NULL);
5034 #else
5035 XSetWindowBackground(DISP, mane.win, bg_current->pixel);
5036 #endif
5037 XClearWindow(DISP, mane.win);
5038 }
5039
5040 fg_current->palette_good = True;
5041 }
5042
5043 if (globals.debug & DBG_DVI)
5044 printf("do_color_change: fg = %ld, bg = %ld, using_planes = %d\n",
5045 fg_current->palette[15], bg_current->pixel, using_planes);
5046
5047 if (using_planes) {
5048 globals.gc.rule = set_or_make_gc(globals.gc.rule, GXor, fg_current->palette[15],
5049 bg_current->pixel);
5050 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXor, fg_current->palette[15],
5051 bg_current->pixel);
5052 }
5053 else {
5054 globals.gc.rule = set_or_make_gc(globals.gc.rule, GXcopy, fg_current->palette[15],
5055 bg_current->pixel);
5056 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXcopy, fg_current->palette[15],
5057 bg_current->pixel);
5058 }
5059
5060 globals.gc.fore2 = NULL;
5061
5062 if (mane.shrinkfactor > 1) {
5063 if (shrink_allocated_for < mane.shrinkfactor) {
5064 if (pixeltbl != NULL)
5065 free((char *) pixeltbl);
5066 pixeltbl = xmalloc((unsigned)(mane.shrinkfactor * mane.shrinkfactor + 1)
5067 * sizeof *pixeltbl);
5068 shrink_allocated_for = mane.shrinkfactor;
5069 }
5070
5071 for (i = 0; i <= mane.shrinkfactor * mane.shrinkfactor; ++i) {
5072 pixeltbl[i] = fg_current->palette[(i * 30 + mane.shrinkfactor * mane.shrinkfactor)
5073 / (2 * mane.shrinkfactor * mane.shrinkfactor)];
5074 }
5075 }
5076 }
5077 } /* end if resource.use_grey */
5078 else
5079 #endif /* GREY */
5080 {
5081 if (!fg_current->pixel_good) {
5082 fg_current->pixel = alloc_color(&fg_current->color,
5083 color_data[0].pixel);
5084 fg_current->pixel_good = True;
5085 }
5086
5087 if (globals.debug & DBG_DVI)
5088 printf("do_color_change: fg = %lx, bg = %lx\n",
5089 fg_current->pixel, bg_current->pixel);
5090
5091 globals.gc.rule = set_or_make_gc(globals.gc.rule, GXcopy, fg_current->pixel, bg_current->pixel);
5092
5093 set_bits = (Pixel) (fg_current->pixel & ~bg_current->pixel);
5094 clr_bits = (Pixel) (bg_current->pixel & ~fg_current->pixel);
5095 globals.gc.fore2 = NULL;
5096
5097 if (resource.copy
5098 || (set_bits && clr_bits && !resource.thorough)) {
5099 if (!resource.copy) {
5100 /* I used to get a warning here which I didn't get for
5101 xdvi-22.64/events.c, l.1330, but I can't reproduce
5102 it any more ...
5103 */
5104 warn_overstrike();
5105 }
5106 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXcopy, fg_current->pixel, bg_current->pixel);
5107 }
5108 else {
5109 if (set_bits) {
5110 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXor, set_bits, 0);
5111 if (clr_bits) {
5112 globals.gc.fore2 = globals.gc.fore2_bak1 = set_or_make_gc(globals.gc.fore2_bak1, GXandInverted, clr_bits, 0);
5113 }
5114 }
5115 else
5116 globals.gc.fore = set_or_make_gc(globals.gc.fore, GXandInverted, clr_bits, 0);
5117 }
5118 }
5119
5120 fg_active = fg_current;
5121 }
5122
5123 #elif GREY /* if COLOR */
5124
5125 void
5126 init_pix(void)
5127 {
5128 static int shrink_allocated_for = 0;
5129 static float oldgamma = 0.0;
5130 static Pixel palette[17];
5131 int i;
5132
5133 if (G_visual->class == TrueColor) {
5134 /* This mirrors the non-grey code in xdvi.c */
5135 static int shift1_r, shift1_g, shift1_b;
5136 static int shift2_r, shift2_g, shift2_b;
5137 static Pixel set_bits;
5138 static Pixel clr_bits;
5139 unsigned int sf_squared;
5140
5141 if (oldgamma == 0.0) {
5142 mask_shifts(G_visual->red_mask, &shift1_r, &shift2_r);
5143 mask_shifts(G_visual->green_mask, &shift1_g, &shift2_g);
5144 mask_shifts(G_visual->blue_mask, &shift1_b, &shift2_b);
5145
5146 set_bits = color_data[0].pixel & ~(color_data[1].pixel);
5147 clr_bits = color_data[1].pixel & ~(color_data[0].pixel);
5148
5149 if (set_bits & G_visual->red_mask)
5150 set_bits |= G_visual->red_mask;
5151 if (clr_bits & G_visual->red_mask)
5152 clr_bits |= G_visual->red_mask;
5153 if (set_bits & G_visual->green_mask)
5154 set_bits |= G_visual->green_mask;
5155 if (clr_bits & G_visual->green_mask)
5156 clr_bits |= G_visual->green_mask;
5157 if (set_bits & G_visual->blue_mask)
5158 set_bits |= G_visual->blue_mask;
5159 if (clr_bits & G_visual->blue_mask)
5160 clr_bits |= G_visual->blue_mask;
5161
5162 /*
5163 * Make the GCs
5164 */
5165
5166 globals.gc.fore = globals.gc.fore2 = globals.gc.rule = 0;
5167 globals.gc.copy = set_or_make_gc(NULL, GXcopy, resource.fore_Pixel, resource.back_Pixel);
5168 if (globals.gc.do_copy || (set_bits && clr_bits)) {
5169 globals.gc.rule = globals.gc.copy;
5170 if (!resource.thorough)
5171 globals.gc.do_copy = True;
5172 }
5173 if (globals.gc.do_copy) {
5174 globals.gc.fore = globals.gc.rule;
5175 if (!resource.copy) {
5176 warn_overstrike();
5177 }
5178 }
5179 else {
5180 if (set_bits) {
5181 globals.gc.fore = set_or_make_gc(NULL, GXor, set_bits & color_data[0].pixel, 0);
5182 }
5183 if (clr_bits || !set_bits) {
5184 /* fprintf(stderr, "using GXandInverted!\n"); */
5185 *(globals.gc.fore ? &globals.gc.fore2 : &globals.gc.fore) =
5186 set_or_make_gc(NULL, GXandInverted, clr_bits & ~(color_data[0].pixel), 0);
5187 }
5188 if (!globals.gc.rule)
5189 globals.gc.rule = globals.gc.fore;
5190 }
5191
5192 oldgamma = resource.gamma;
5193 }
5194
5195 if (mane.shrinkfactor == 1)
5196 return;
5197 sf_squared = mane.shrinkfactor * mane.shrinkfactor;
5198
5199 if (shrink_allocated_for < mane.shrinkfactor) {
5200 if (pixeltbl != NULL) {
5201 free((char *)pixeltbl);
5202 if (pixeltbl_gc2 != NULL)
5203 free((char *)pixeltbl_gc2);
5204 }
5205 pixeltbl = xmalloc((sf_squared + 1) * sizeof *pixeltbl);
5206 shrink_allocated_for = mane.shrinkfactor;
5207 if (globals.gc.fore2 != NULL) {
5208 pixeltbl_gc2 = xmalloc((sf_squared + 1) * sizeof *pixeltbl_gc2);
5209 }
5210 }
5211
5212 /*
5213 * Initialize the pixel lookup table according to the gamma values.
5214 */
5215 #define SHIFTIFY(x, shift1, shift2) ((((Pixel)(x)) >> (shift1)) << (shift2))
5216
5217 for (i = 0; i <= sf_squared; ++i) {
5218 double frac = resource.gamma > 0
5219 ? pow((double)i / sf_squared, 1 / resource.gamma)
5220 : 1 - pow((double)(sf_squared - i) / sf_squared, -resource.gamma);
5221 unsigned int red, green, blue;
5222 Pixel pixel;
5223
5224 red = frac * ((double)color_data[0].red - color_data[1].red)
5225 + color_data[1].red;
5226 green = frac
5227 * ((double)color_data[0].green - color_data[1].green)
5228 + color_data[1].green;
5229 blue = frac * ((double)color_data[0].blue - color_data[1].blue)
5230 + color_data[1].blue;
5231
5232 pixel = SHIFTIFY(red, shift1_r, shift2_r) |
5233 SHIFTIFY(green, shift1_g, shift2_g) |
5234 SHIFTIFY(blue, shift1_b, shift2_b);
5235
5236 if (globals.gc.do_copy) {
5237 pixeltbl[i] = pixel;
5238 }
5239 else if (globals.gc.fore2 != NULL) { /* if thorough */
5240 pixeltbl[i] = pixel & ~(color_data[1].pixel);
5241 pixeltbl_gc2[i] = ~pixel & color_data[1].pixel;
5242 }
5243 else {
5244 pixeltbl[i] = set_bits ? pixel & set_bits : ~pixel & clr_bits;
5245 }
5246 }
5247
5248 #undef SHIFTIFY
5249
5250 return;
5251 }
5252
5253 /* if not TrueColor ... */
5254
5255 if (resource.gamma != oldgamma) {
5256 XColor color;
5257
5258 for (i = 0; i < 16; ++i) {
5259 double frac = resource.gamma > 0
5260 ? pow((double)i / 15, 1 / resource.gamma)
5261 : 1 - pow((double)(15 - i) / 15, -resource.gamma);
5262
5263 color.red = frac
5264 * ((double)color_data[0].red - color_data[1].red)
5265 + color_data[1].red;
5266 color.green = frac
5267 * ((double)color_data[0].green - color_data[1].green)
5268 + color_data[1].green;
5269 color.blue = frac
5270 * ((double)color_data[0].blue - color_data[1].blue)
5271 + color_data[1].blue;
5272
5273 color.pixel = resource.back_Pixel;
5274 color.flags = DoRed | DoGreen | DoBlue;
5275
5276 if (!globals.gc.do_copy) {
5277 if (i & 1)
5278 color.pixel |= plane_masks[0];
5279 if (i & 2)
5280 color.pixel |= plane_masks[1];
5281 if (i & 4)
5282 color.pixel |= plane_masks[2];
5283 if (i & 8)
5284 color.pixel |= plane_masks[3];
5285 XStoreColor(DISP, G_colormap, &color);
5286 palette[i] = color.pixel;
5287 }
5288 else {
5289 if (XAllocColor(DISP, G_colormap, &color))
5290 palette[i] = color.pixel;
5291 else
5292 palette[i] = (i * 100 >= resource.density * 15)
5293 ? resource.fore_Pixel : resource.back_Pixel;
5294 }
5295 }
5296
5297 globals.gc.copy = set_or_make_gc(NULL, GXcopy, resource.fore_Pixel, resource.back_Pixel);
5298 globals.gc.rule = globals.gc.do_copy
5299 ? globals.gc.copy
5300 : set_or_make_gc(NULL, GXor, resource.fore_Pixel, resource.back_Pixel);
5301 globals.gc.fore = globals.gc.rule;
5302 globals.gc.fore2 = NULL;
5303 oldgamma = resource.gamma;
5304 }
5305
5306 if (mane.shrinkfactor == 1)
5307 return;
5308
5309 if (shrink_allocated_for < mane.shrinkfactor) {
5310 if (pixeltbl != NULL)
5311 free((char *)pixeltbl);
5312 pixeltbl = xmalloc((unsigned)
5313 (mane.shrinkfactor * mane.shrinkfactor + 1) * sizeof *pixeltbl);
5314 shrink_allocated_for = mane.shrinkfactor;
5315 }
5316
5317 for (i = 0; i <= mane.shrinkfactor * mane.shrinkfactor; ++i) {
5318 pixeltbl[i] = palette[(i * 30 + mane.shrinkfactor * mane.shrinkfactor)
5319 / (2 * mane.shrinkfactor * mane.shrinkfactor)];
5320 }
5321 }
5322
5323 #endif /* COLOR */