summaryrefslogtreecommitdiff
path: root/sys/dev/rcons/raster_text.c
blob: 5b0188c1296ddd107c6ae6306f4fc48a9a1bb8c6 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*	$OpenBSD: raster_text.c,v 1.3 1997/11/07 08:07:39 niklas Exp $ */
/*	$NetBSD: raster_text.c,v 1.3 1995/11/24 23:50:56 cgd Exp $ */

/*-
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to the Computer Systems
 * Engineering Group at Lawrence Berkeley Laboratory and to the University
 * of California at Berkeley by Jef Poskanzer.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)raster_text.c	8.1 (Berkeley) 6/11/93
 */

/*
 * Text routines for raster library.
 */

#ifdef _KERNEL
#include <sys/param.h>
#include <sys/systm.h>
#include <dev/rcons/raster.h>
#ifdef COLORFONT_CACHE
#include <sys/malloc.h>
#define NEW(size) malloc(size, M_DEVBUF, M_NOWAIT)
#endif
#else
#include <sys/types.h>
#include <string.h>
#include "raster.h"
#ifdef COLORFONT_CACHE
#include <malloc.h>
#define NEW(size) malloc(size)
#endif
#endif

/* Draws text.  Returns 0 on success, -1 on failure. */
int
raster_text( r, x, y, rop, rf, text )
    register struct raster* r;
    int x, y;
    int rop;
    struct raster_font* rf;
    unsigned char* text;
    {
    return raster_textn( r, x, y, rop, rf, text, strlen( text ) );
    }

/* Draws n characters of text.  Returns 0 on success, -1 on failure. */
int
raster_textn( r, x, y, rop, rf, text, n )
    register struct raster* r;
    int x, y;
    int rop;
    struct raster_font* rf;
    unsigned char* text;
    int n;
    {
    int clip;
    int x1, y1;
    struct raster_char* c;
    struct raster* charrast;
    int i;
    register unsigned char ch;
    int thisx, thisy;
    int phase;

    /* Check whether we can avoid clipping. */
    clip = 0;
    if ( rf->flags & RASFONT_FIXEDWIDTH &&
	 rf->flags & RASFONT_NOVERTICALMOVEMENT )
	{
	/* This font is well-behaved, we can compute the extent cheaply. */
	c = &(rf->chars['@']);
	charrast = c->r;
	if ( x + c->homex < 0 || y + c->homey < 0 ||
	     x + c->homex + n * c->nextx > r->width ||
	     y + c->homey + charrast->height > r->height )
	    clip = 1;
	}
    else
	{
	/* Got to step through the string to compute the extent. */
	for ( i = 0, x1 = x, y1 = y;
	      i < n;
	      ++i, x1 += c->nextx, y1 += c->nexty )
	    {
	    c = &(rf->chars[text[i]]);
	    charrast = c->r;
	    if ( charrast != (struct raster*) 0 )
		{
		if ( x1 + c->homex < 0 || y1 + c->homey < 0 ||
		     x1 + c->homex + charrast->width > r->width ||
		     y1 + c->homey + charrast->height > r->height )
		    {
		    clip = 1;
		    break;
		    }
		}
	    }
	}

    /* Now display the text. */
    for ( i = 0, x1 = x, y1 = y;
	  i < n;
	  ++i, x1 += c->nextx, y1 += c->nexty )
	{
	ch = text[i];
	c = &(rf->chars[ch]);
	charrast = c->r;
	if ( charrast != (struct raster*) 0 )
	    {
	    thisx = x1 + c->homex;
	    thisy = y1 + c->homey;

	    phase = 0;
#ifdef COLORFONT_CACHE
	    if ( r->depth == 8 )
		{
		/* Initialize color font cache if necessary. */
		if ( rf->cache == (struct raster_fontcache*) -1 )
		    {
		    int c;

		    rf->cache = (struct raster_fontcache*)
			NEW( sizeof(struct raster_fontcache) );
		    if ( rf->cache != (struct raster_fontcache*) 0 )
			for ( c = 0; c < 256; ++c )
			    rf->cache->cr[c] = (struct raster*) 0;
		    }

		if ( rf->cache != (struct raster_fontcache*) 0 )
		    {
		    int color;
		    struct raster* cr;

		    color = RAS_GETCOLOR( rop );
		    cr = rf->cache->cr[ch];
		    /* Is this character cached yet? */
		    if ( cr != (struct raster*) 0 )
			{
			/* Yes, but is it the right color? */
			if ( rf->cache->color[ch] == color )
			    {
			    /* Yes - switch rasters. */
			    charrast = cr;
			    }
			else
			    {
			    /* No, re-draw it. */
			    if ( raster_op_noclip(
				 cr, 0, 0, charrast->width,
				 charrast->height, rop, charrast, 0, 0 ) == 0 )
				{
				rf->cache->color[ch] = color;
				charrast = cr;
				}
			    }
			}
		    else
			{
			/* It's not cached, so cache it. */
			cr = raster_alloc(
			    charrast->width, charrast->height, 8 );
			if ( cr != (struct raster*) 0 )
			    if ( raster_op_noclip(
				 cr, 0, 0, charrast->width, charrast->height,
				 rop, charrast, 0, 0 ) == 0 )
				{
				rf->cache->color[ch] = color;
				charrast = rf->cache->cr[ch] = cr;
				}
			}
		    }
		}
#endif /*COLORFONT_CACHE*/

	    if ( clip )
		{
		if ( raster_op(
			 r, thisx, thisy, charrast->width, charrast->height,
			 rop, charrast, phase, 0 ) < 0 )
		    return -1;
		}
	    else
		{
		if ( raster_op_noclip(
			 r, thisx, thisy, charrast->width, charrast->height,
			 rop, charrast, phase, 0 ) < 0 )
		    return -1;
		}
	    }
	}

    return 0;
    }

#ifdef COLORFONT_CACHE
/* Allocates a raster.  Returns (struct raster*) 0 on failure. */
struct raster*
raster_alloc( width, height, depth )
    int width, height, depth;
    {
    struct raster* r;
    int linelongs;

    if ( width <= 0 || height <= 0 || ( depth != 1 && depth != 8 ) )
	return (struct raster*) 0;
    linelongs = ( ( width * depth + 31 ) >> 5 );
    r = (struct raster*)
	NEW( sizeof(struct raster) + height * linelongs * sizeof(u_int32_t));
    if ( r == (struct raster*) 0 )
	return (struct raster*) 0;

    r->width = width;
    r->height = height;
    r->depth = depth;
    r->linelongs = linelongs;
    r->pixels = (u_int32_t*) (r + 1);
    r->data = (caddr_t) 0;
    return r;
    }
#endif