File: gp_dosfb.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (194 lines) | stat: -rw-r--r-- 5,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* gp_dosfb.c */
/* MS-DOS frame buffer swapping routines for Ghostscript */
#include <conio.h>
#include "malloc_.h"
#include "memory_.h"
#include "gx.h"
#include "gp.h"
#include "gserrors.h"
#include "gxdevice.h"

/* On MS-DOS machines, we maintain a console image in memory, */
/* and swap screens on request. */
#define cw_width 80
#define cw_height 25
typedef struct text_line_s {
	int end;
	char text[cw_width + 1];
} text_line;
typedef struct {
	text_line *line;
	text_line lines[cw_height];
} ds_text_screen;

private ds_text_screen *console;

private int console_is_current;

/* Buffer one scan line of graphics. */
#define row_buf_size 1280
private char graphics_file_name[] = "_temp_.gfb";

/* Forward references */
private int save_graphics(P1(gx_device *));
private int restore_graphics(P1(gx_device *));

/* Initialize the console buffer. */
void
gp_init_console(void)
{	console = (ds_text_screen *)gs_malloc(1, sizeof(ds_text_screen), "gp_init_console(dosfb)");
	if ( console != 0 )
	{	memset(&console->lines, 0, sizeof(console->lines));
		console->line = &console->lines[0];
		console_is_current = 0;
	}
	else
		console_is_current = 1;
}

/* Write a string to the console. */
void
gp_console_puts(const char *str, uint size)
{	register ds_text_screen *cop = console;
	register text_line *lip;
	if ( console == 0 )
	{	fwrite(str, 1, size, stdout);
		return;
	}
	lip = cop->line;
	for ( ; size ; str++, size-- )
	  switch ( *str )
	   {
	case '\n':
		if ( lip == &cop->lines[cw_height - 1] )
		   {	/* Scroll up */
			memcpy(&cop->lines[0], &cop->lines[1],
			       sizeof(text_line) * (cw_height - 1));
		   }
		else
			cop->line = ++lip;
		lip->end = 0;
		break;
	case '\t':
		gp_console_puts("        ", 8 - (lip->end & 7));
		lip = cop->line;
		break;
	default:
		if ( lip->end == cw_width )
		   {	gp_console_puts("\n", 1);
			lip = cop->line;
		   }
		lip->text[lip->end++] = *str;
	   }
}

/* Make the console current on the screen. */
int
gp_make_console_current(gx_device *dev)
{	int code = 0;
	if ( console == 0 )
		return 0;
	if ( !console_is_current )
		code = save_graphics(dev);
	/* Transfer the console buffer to the screen. */
	/* Unfortunately, there is no standard way to clear the screen. */
	/* Output the ANSI sequence and hope for the best. */
	cputs("\r\033[2J\r    \r");
	   {	int i;
		register text_line *lip;
		for ( i = 0, lip = &console->lines[0]; i < cw_height; i++, lip++ )
		   {	if ( i != 0 ) cputs("\r\n");
			lip->text[lip->end] = 0;
			cputs(lip->text);
		   }
	   }
	console_is_current = 1;
	return code;
}

/* Make the graphics current on the screen. */
int
gp_make_graphics_current(gx_device *dev)
{	if ( console == 0 )
		return 0;
	if ( console_is_current )
	   {	int code = restore_graphics(dev);
		if ( code < 0 ) return code;
		console_is_current = 0;
	   }
	return 0;
}

/* ------ Internal routines ------ */

/* We compress the pixmap just a little, by noting */
/* replicated bytes at the beginning and end of a line. */
typedef struct { ushort pre, post; } row_head;

/* Save the graphics screen on a file. */
private int
save_graphics(gx_device *dev)
{	uint row_size = gx_device_raster(dev, 0);
	char row_buf[row_buf_size];
	FILE *gfile;
	int y;
	if ( row_size > row_buf_size ) return -1;
	gfile = fopen(graphics_file_name, "wb");
	if ( gfile == 0 ) return gs_error_ioerror;
	for ( y = 0; y < dev->height; y++ )
	   {	char _ss *row = row_buf;
		(*dev_proc(dev, get_bits))(dev, y, row, NULL);
		   {	row_head head;
			char _ss *beg = row, *end = row + row_size - 1;
			while ( end > beg && *end == end[-1] ) end--;
			if ( beg < end )
				while ( *beg == beg[1] ) beg++;
			head.pre = beg - row;
			head.post = end + 1 - row;
			fwrite((char *)&head, 1, sizeof(head), gfile);
			fwrite(beg, head.post - head.pre, 1, gfile);
			row += row_size;
		   }
	   }
	fclose(gfile);
	return 0;
}

/* Restore the graphics screen from a file. */
private int
restore_graphics(gx_device *dev)
{	FILE *gfile;
	uint row_size = gx_device_raster(dev, 0);
	char row_buf[row_buf_size];
	int y;
	if ( row_size > row_buf_size ) return -1;
	gfile = fopen(graphics_file_name, "rb");
	if ( gfile == 0 ) return gs_error_ioerror;
	for ( y = 0; y < dev->height; y ++ )
	   {	row_head head;
		char _ss *beg, *end;
		fread((char *)&head, 1, sizeof(head), gfile);
		beg = row_buf + head.pre;
		end = row_buf + head.post;
		fread(beg, 1, end - beg, gfile);
		if ( head.pre )
			memset(row_buf, *beg, head.pre);
		if ( head.post < row_size )
			memset(end, end[-1], row_size - head.post);
		(*dev_proc(dev, copy_color))(dev, row_buf, 0, row_size, gx_no_bitmap_id, 0, y, dev->width, 1);
	   }
	fclose(gfile);
	return 0;
}