summaryrefslogtreecommitdiff
path: root/gs/src/gp_dosfb.c
blob: 9704998856f997eebc037aa69f0403de726da66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (C) 1992, 1997, 1998 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */


/* 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;

/* Allocate the console buffer statically. */
private ds_text_screen gp_console;

#define console (&gp_console)
private bool console_initialized = false;
private bool console_is_current;

/* Buffer one scan line of graphics. */
#define row_buf_size 1280
private const 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)
{
    memset(&console->lines, 0, sizeof(console->lines));
    console->line = &console->lines[0];
    console_is_current = false;
    console_initialized = true;
}

/* 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_initialized) {
	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_initialized)
	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 = true;
    return code;
}

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

	if (code < 0)
	    return code;
	console_is_current = false;
    }
    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 *row = row_buf;

	(*dev_proc(dev, get_bits)) (dev, y, row, NULL);
	{
	    row_head head;
	    char *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 *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;
}