1 /* $NetBSD: raster_text.c,v 1.8 2005/12/11 12:23:44 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to the Computer Systems
8 * Engineering Group at Lawrence Berkeley Laboratory and to the University
9 * of California at Berkeley by Jef Poskanzer.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)raster_text.c 8.1 (Berkeley) 6/11/93
39 * Text routines for raster library.
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: raster_text.c,v 1.8 2005/12/11 12:23:44 christos Exp $");
45 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <dev/rcons/raster.h>
49 #ifdef COLORFONT_CACHE
50 #include <sys/malloc.h>
51 #define NEW(size) malloc(size, M_DEVBUF, M_NOWAIT)
56 #ifdef COLORFONT_CACHE
58 #define NEW(size) malloc(size)
62 /* Draws text. Returns 0 on success, -1 on failure. */
64 raster_text( r
, x
, y
, rop
, rf
, text
)
68 struct raster_font
* rf
;
71 return raster_textn( r
, x
, y
, rop
, rf
, text
, strlen( text
) );
74 /* Draws n characters of text. Returns 0 on success, -1 on failure. */
76 raster_textn( r
, x
, y
, rop
, rf
, text
, n
)
80 struct raster_font
* rf
;
86 struct raster_char
* c
;
87 struct raster
* charrast
;
93 /* Check whether we can avoid clipping. */
95 if ( rf
->flags
& RASFONT_FIXEDWIDTH
&&
96 rf
->flags
& RASFONT_NOVERTICALMOVEMENT
)
98 /* This font is well-behaved, we can compute the extent cheaply. */
99 c
= &(rf
->chars
['@']);
101 if ( x
+ c
->homex
< 0 || y
+ c
->homey
< 0 ||
102 x
+ c
->homex
+ n
* c
->nextx
> r
->width
||
103 y
+ c
->homey
+ charrast
->height
> r
->height
)
108 /* Got to step through the string to compute the extent. */
109 for ( i
= 0, x1
= x
, y1
= y
;
111 ++i
, x1
+= c
->nextx
, y1
+= c
->nexty
)
113 c
= &(rf
->chars
[text
[i
]]);
115 if ( charrast
!= (struct raster
*) 0 )
117 if ( x1
+ c
->homex
< 0 || y1
+ c
->homey
< 0 ||
118 x1
+ c
->homex
+ charrast
->width
> r
->width
||
119 y1
+ c
->homey
+ charrast
->height
> r
->height
)
128 /* Now display the text. */
129 for ( i
= 0, x1
= x
, y1
= y
;
131 ++i
, x1
+= c
->nextx
, y1
+= c
->nexty
)
134 c
= &(rf
->chars
[ch
]);
136 if ( charrast
!= (struct raster
*) 0 )
138 thisx
= x1
+ c
->homex
;
139 thisy
= y1
+ c
->homey
;
142 #ifdef COLORFONT_CACHE
145 /* Initialize color font cache if necessary. */
146 if ( rf
->cache
== (struct raster_fontcache
*) -1 )
150 rf
->cache
= (struct raster_fontcache
*)
151 NEW( sizeof(struct raster_fontcache
) );
152 if ( rf
->cache
!= (struct raster_fontcache
*) 0 )
153 for ( c
= 0; c
< 256; ++c
)
154 rf
->cache
->cr
[c
] = (struct raster
*) 0;
157 if ( rf
->cache
!= (struct raster_fontcache
*) 0 )
162 color
= RAS_GETCOLOR( rop
);
163 cr
= rf
->cache
->cr
[ch
];
164 /* Is this character cached yet? */
165 if ( cr
!= (struct raster
*) 0 )
167 /* Yes, but is it the right color? */
168 if ( rf
->cache
->color
[ch
] == color
)
170 /* Yes - switch rasters. */
175 /* No, re-draw it. */
176 if ( raster_op_noclip(
177 cr
, 0, 0, charrast
->width
,
178 charrast
->height
, rop
, charrast
, 0, 0 ) == 0 )
180 rf
->cache
->color
[ch
] = color
;
187 /* It's not cached, so cache it. */
189 charrast
->width
, charrast
->height
, 8 );
190 if ( cr
!= (struct raster
*) 0 )
191 if ( raster_op_noclip(
192 cr
, 0, 0, charrast
->width
, charrast
->height
,
193 rop
, charrast
, 0, 0 ) == 0 )
195 rf
->cache
->color
[ch
] = color
;
196 charrast
= rf
->cache
->cr
[ch
] = cr
;
201 #endif /*COLORFONT_CACHE*/
206 r
, thisx
, thisy
, charrast
->width
, charrast
->height
,
207 rop
, charrast
, phase
, 0 ) < 0 )
212 if ( raster_op_noclip(
213 r
, thisx
, thisy
, charrast
->width
, charrast
->height
,
214 rop
, charrast
, phase
, 0 ) < 0 )
223 #ifdef COLORFONT_CACHE
224 /* Allocates a raster. Returns (struct raster*) 0 on failure. */
226 raster_alloc( width
, height
, depth
)
227 int width
, height
, depth
;
232 if ( width
<= 0 || height
<= 0 || ( depth
!= 1 && depth
!= 8 ) )
233 return (struct raster
*) 0;
234 linelongs
= ( ( width
* depth
+ 31 ) >> 5 );
236 NEW( sizeof(struct raster
) + height
* linelongs
* sizeof(u_int32_t
));
237 if ( r
== (struct raster
*) 0 )
238 return (struct raster
*) 0;
243 r
->linelongs
= linelongs
;
244 r
->pixels
= (u_int32_t
*) (r
+ 1);
245 r
->data
= (void *) 0;