raster_text.c revision 1.6 1 1.6 lukem /* $NetBSD: raster_text.c,v 1.6 2001/11/15 09:48:15 lukem Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1991, 1993
5 1.1 pk * The Regents of the University of California. All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to the Computer Systems
8 1.1 pk * Engineering Group at Lawrence Berkeley Laboratory and to the University
9 1.1 pk * of California at Berkeley by Jef Poskanzer.
10 1.1 pk *
11 1.1 pk * Redistribution and use in source and binary forms, with or without
12 1.1 pk * modification, are permitted provided that the following conditions
13 1.1 pk * are met:
14 1.1 pk * 1. Redistributions of source code must retain the above copyright
15 1.1 pk * notice, this list of conditions and the following disclaimer.
16 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pk * notice, this list of conditions and the following disclaimer in the
18 1.1 pk * documentation and/or other materials provided with the distribution.
19 1.1 pk * 3. All advertising materials mentioning features or use of this software
20 1.1 pk * must display the following acknowledgement:
21 1.1 pk * This product includes software developed by the University of
22 1.1 pk * California, Berkeley and its contributors.
23 1.1 pk * 4. Neither the name of the University nor the names of its contributors
24 1.1 pk * may be used to endorse or promote products derived from this software
25 1.1 pk * without specific prior written permission.
26 1.1 pk *
27 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 pk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 pk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 pk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 pk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 pk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 pk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 pk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 pk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 pk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 pk * SUCH DAMAGE.
38 1.1 pk *
39 1.1 pk * @(#)raster_text.c 8.1 (Berkeley) 6/11/93
40 1.1 pk */
41 1.1 pk
42 1.1 pk /*
43 1.1 pk * Text routines for raster library.
44 1.1 pk */
45 1.5 lukem
46 1.5 lukem #include <sys/cdefs.h>
47 1.6 lukem __KERNEL_RCSID(0, "$NetBSD: raster_text.c,v 1.6 2001/11/15 09:48:15 lukem Exp $");
48 1.1 pk
49 1.6 lukem #include <sys/param.h>
50 1.1 pk #ifdef _KERNEL
51 1.3 cgd #include <sys/systm.h>
52 1.1 pk #include <dev/rcons/raster.h>
53 1.1 pk #ifdef COLORFONT_CACHE
54 1.1 pk #include <sys/malloc.h>
55 1.1 pk #define NEW(size) malloc(size, M_DEVBUF, M_NOWAIT)
56 1.1 pk #endif
57 1.1 pk #else
58 1.3 cgd #include <string.h>
59 1.3 cgd #include "raster.h"
60 1.1 pk #ifdef COLORFONT_CACHE
61 1.1 pk #include <malloc.h>
62 1.1 pk #define NEW(size) malloc(size)
63 1.1 pk #endif
64 1.1 pk #endif
65 1.1 pk
66 1.1 pk /* Draws text. Returns 0 on success, -1 on failure. */
67 1.1 pk int
68 1.1 pk raster_text( r, x, y, rop, rf, text )
69 1.4 augustss struct raster* r;
70 1.1 pk int x, y;
71 1.1 pk int rop;
72 1.1 pk struct raster_font* rf;
73 1.2 pk unsigned char* text;
74 1.1 pk {
75 1.1 pk return raster_textn( r, x, y, rop, rf, text, strlen( text ) );
76 1.1 pk }
77 1.1 pk
78 1.1 pk /* Draws n characters of text. Returns 0 on success, -1 on failure. */
79 1.1 pk int
80 1.1 pk raster_textn( r, x, y, rop, rf, text, n )
81 1.4 augustss struct raster* r;
82 1.1 pk int x, y;
83 1.1 pk int rop;
84 1.1 pk struct raster_font* rf;
85 1.2 pk unsigned char* text;
86 1.1 pk int n;
87 1.1 pk {
88 1.1 pk int clip;
89 1.1 pk int x1, y1;
90 1.1 pk struct raster_char* c;
91 1.1 pk struct raster* charrast;
92 1.1 pk int i;
93 1.4 augustss unsigned char ch;
94 1.1 pk int thisx, thisy;
95 1.1 pk int phase;
96 1.1 pk
97 1.1 pk /* Check whether we can avoid clipping. */
98 1.1 pk clip = 0;
99 1.1 pk if ( rf->flags & RASFONT_FIXEDWIDTH &&
100 1.1 pk rf->flags & RASFONT_NOVERTICALMOVEMENT )
101 1.1 pk {
102 1.1 pk /* This font is well-behaved, we can compute the extent cheaply. */
103 1.1 pk c = &(rf->chars['@']);
104 1.1 pk charrast = c->r;
105 1.1 pk if ( x + c->homex < 0 || y + c->homey < 0 ||
106 1.1 pk x + c->homex + n * c->nextx > r->width ||
107 1.1 pk y + c->homey + charrast->height > r->height )
108 1.1 pk clip = 1;
109 1.1 pk }
110 1.1 pk else
111 1.1 pk {
112 1.1 pk /* Got to step through the string to compute the extent. */
113 1.1 pk for ( i = 0, x1 = x, y1 = y;
114 1.1 pk i < n;
115 1.1 pk ++i, x1 += c->nextx, y1 += c->nexty )
116 1.1 pk {
117 1.1 pk c = &(rf->chars[text[i]]);
118 1.1 pk charrast = c->r;
119 1.1 pk if ( charrast != (struct raster*) 0 )
120 1.1 pk {
121 1.1 pk if ( x1 + c->homex < 0 || y1 + c->homey < 0 ||
122 1.1 pk x1 + c->homex + charrast->width > r->width ||
123 1.1 pk y1 + c->homey + charrast->height > r->height )
124 1.1 pk {
125 1.1 pk clip = 1;
126 1.1 pk break;
127 1.1 pk }
128 1.1 pk }
129 1.1 pk }
130 1.1 pk }
131 1.1 pk
132 1.1 pk /* Now display the text. */
133 1.1 pk for ( i = 0, x1 = x, y1 = y;
134 1.1 pk i < n;
135 1.1 pk ++i, x1 += c->nextx, y1 += c->nexty )
136 1.1 pk {
137 1.1 pk ch = text[i];
138 1.1 pk c = &(rf->chars[ch]);
139 1.1 pk charrast = c->r;
140 1.1 pk if ( charrast != (struct raster*) 0 )
141 1.1 pk {
142 1.1 pk thisx = x1 + c->homex;
143 1.1 pk thisy = y1 + c->homey;
144 1.1 pk
145 1.1 pk phase = 0;
146 1.1 pk #ifdef COLORFONT_CACHE
147 1.1 pk if ( r->depth == 8 )
148 1.1 pk {
149 1.1 pk /* Initialize color font cache if necessary. */
150 1.1 pk if ( rf->cache == (struct raster_fontcache*) -1 )
151 1.1 pk {
152 1.1 pk int c;
153 1.1 pk
154 1.1 pk rf->cache = (struct raster_fontcache*)
155 1.1 pk NEW( sizeof(struct raster_fontcache) );
156 1.1 pk if ( rf->cache != (struct raster_fontcache*) 0 )
157 1.1 pk for ( c = 0; c < 256; ++c )
158 1.1 pk rf->cache->cr[c] = (struct raster*) 0;
159 1.1 pk }
160 1.1 pk
161 1.1 pk if ( rf->cache != (struct raster_fontcache*) 0 )
162 1.1 pk {
163 1.1 pk int color;
164 1.1 pk struct raster* cr;
165 1.1 pk
166 1.1 pk color = RAS_GETCOLOR( rop );
167 1.1 pk cr = rf->cache->cr[ch];
168 1.1 pk /* Is this character cached yet? */
169 1.1 pk if ( cr != (struct raster*) 0 )
170 1.1 pk {
171 1.1 pk /* Yes, but is it the right color? */
172 1.1 pk if ( rf->cache->color[ch] == color )
173 1.1 pk {
174 1.1 pk /* Yes - switch rasters. */
175 1.1 pk charrast = cr;
176 1.1 pk }
177 1.1 pk else
178 1.1 pk {
179 1.1 pk /* No, re-draw it. */
180 1.1 pk if ( raster_op_noclip(
181 1.1 pk cr, 0, 0, charrast->width,
182 1.1 pk charrast->height, rop, charrast, 0, 0 ) == 0 )
183 1.1 pk {
184 1.1 pk rf->cache->color[ch] = color;
185 1.1 pk charrast = cr;
186 1.1 pk }
187 1.1 pk }
188 1.1 pk }
189 1.1 pk else
190 1.1 pk {
191 1.1 pk /* It's not cached, so cache it. */
192 1.1 pk cr = raster_alloc(
193 1.1 pk charrast->width, charrast->height, 8 );
194 1.1 pk if ( cr != (struct raster*) 0 )
195 1.1 pk if ( raster_op_noclip(
196 1.1 pk cr, 0, 0, charrast->width, charrast->height,
197 1.1 pk rop, charrast, 0, 0 ) == 0 )
198 1.1 pk {
199 1.1 pk rf->cache->color[ch] = color;
200 1.1 pk charrast = rf->cache->cr[ch] = cr;
201 1.1 pk }
202 1.1 pk }
203 1.1 pk }
204 1.1 pk }
205 1.1 pk #endif /*COLORFONT_CACHE*/
206 1.1 pk
207 1.1 pk if ( clip )
208 1.1 pk {
209 1.1 pk if ( raster_op(
210 1.1 pk r, thisx, thisy, charrast->width, charrast->height,
211 1.1 pk rop, charrast, phase, 0 ) < 0 )
212 1.1 pk return -1;
213 1.1 pk }
214 1.1 pk else
215 1.1 pk {
216 1.1 pk if ( raster_op_noclip(
217 1.1 pk r, thisx, thisy, charrast->width, charrast->height,
218 1.1 pk rop, charrast, phase, 0 ) < 0 )
219 1.1 pk return -1;
220 1.1 pk }
221 1.1 pk }
222 1.1 pk }
223 1.1 pk
224 1.1 pk return 0;
225 1.1 pk }
226 1.1 pk
227 1.1 pk #ifdef COLORFONT_CACHE
228 1.1 pk /* Allocates a raster. Returns (struct raster*) 0 on failure. */
229 1.1 pk struct raster*
230 1.1 pk raster_alloc( width, height, depth )
231 1.1 pk int width, height, depth;
232 1.1 pk {
233 1.1 pk struct raster* r;
234 1.1 pk int linelongs;
235 1.1 pk
236 1.1 pk if ( width <= 0 || height <= 0 || ( depth != 1 && depth != 8 ) )
237 1.1 pk return (struct raster*) 0;
238 1.1 pk linelongs = ( ( width * depth + 31 ) >> 5 );
239 1.1 pk r = (struct raster*)
240 1.3 cgd NEW( sizeof(struct raster) + height * linelongs * sizeof(u_int32_t));
241 1.1 pk if ( r == (struct raster*) 0 )
242 1.1 pk return (struct raster*) 0;
243 1.1 pk
244 1.1 pk r->width = width;
245 1.1 pk r->height = height;
246 1.1 pk r->depth = depth;
247 1.1 pk r->linelongs = linelongs;
248 1.3 cgd r->pixels = (u_int32_t*) (r + 1);
249 1.1 pk r->data = (caddr_t) 0;
250 1.1 pk return r;
251 1.1 pk }
252 1.1 pk #endif
253