Home | History | Annotate | Line # | Download | only in rcons
raster_text.c revision 1.1
      1  1.1  pk /*	$NetBSD: raster_text.c,v 1.1 1995/09/17 19:56:35 pk 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.1  pk 
     46  1.1  pk #ifdef _KERNEL
     47  1.1  pk #include <sys/param.h>
     48  1.1  pk #include <dev/rcons/raster.h>
     49  1.1  pk #ifdef COLORFONT_CACHE
     50  1.1  pk #include <sys/malloc.h>
     51  1.1  pk #define NEW(size) malloc(size, M_DEVBUF, M_NOWAIT)
     52  1.1  pk #endif
     53  1.1  pk #else
     54  1.1  pk #include <sys/types.h>
     55  1.1  pk #include <dev/rcons/raster.h>
     56  1.1  pk #ifdef COLORFONT_CACHE
     57  1.1  pk #include <malloc.h>
     58  1.1  pk #define NEW(size) malloc(size)
     59  1.1  pk #endif
     60  1.1  pk #endif
     61  1.1  pk 
     62  1.1  pk 
     63  1.1  pk /* Draws text.  Returns 0 on success, -1 on failure. */
     64  1.1  pk int
     65  1.1  pk raster_text( r, x, y, rop, rf, text )
     66  1.1  pk     register struct raster* r;
     67  1.1  pk     int x, y;
     68  1.1  pk     int rop;
     69  1.1  pk     struct raster_font* rf;
     70  1.1  pk     char* text;
     71  1.1  pk     {
     72  1.1  pk     return raster_textn( r, x, y, rop, rf, text, strlen( text ) );
     73  1.1  pk     }
     74  1.1  pk 
     75  1.1  pk /* Draws n characters of text.  Returns 0 on success, -1 on failure. */
     76  1.1  pk int
     77  1.1  pk raster_textn( r, x, y, rop, rf, text, n )
     78  1.1  pk     register struct raster* r;
     79  1.1  pk     int x, y;
     80  1.1  pk     int rop;
     81  1.1  pk     struct raster_font* rf;
     82  1.1  pk     char* text;
     83  1.1  pk     int n;
     84  1.1  pk     {
     85  1.1  pk     int clip;
     86  1.1  pk     int x1, y1;
     87  1.1  pk     struct raster_char* c;
     88  1.1  pk     struct raster* charrast;
     89  1.1  pk     int i;
     90  1.1  pk     register char ch;
     91  1.1  pk     int thisx, thisy;
     92  1.1  pk     int phase;
     93  1.1  pk 
     94  1.1  pk     /* Check whether we can avoid clipping. */
     95  1.1  pk     clip = 0;
     96  1.1  pk     if ( rf->flags & RASFONT_FIXEDWIDTH &&
     97  1.1  pk 	 rf->flags & RASFONT_NOVERTICALMOVEMENT )
     98  1.1  pk 	{
     99  1.1  pk 	/* This font is well-behaved, we can compute the extent cheaply. */
    100  1.1  pk 	c = &(rf->chars['@']);
    101  1.1  pk 	charrast = c->r;
    102  1.1  pk 	if ( x + c->homex < 0 || y + c->homey < 0 ||
    103  1.1  pk 	     x + c->homex + n * c->nextx > r->width ||
    104  1.1  pk 	     y + c->homey + charrast->height > r->height )
    105  1.1  pk 	    clip = 1;
    106  1.1  pk 	}
    107  1.1  pk     else
    108  1.1  pk 	{
    109  1.1  pk 	/* Got to step through the string to compute the extent. */
    110  1.1  pk 	for ( i = 0, x1 = x, y1 = y;
    111  1.1  pk 	      i < n;
    112  1.1  pk 	      ++i, x1 += c->nextx, y1 += c->nexty )
    113  1.1  pk 	    {
    114  1.1  pk 	    c = &(rf->chars[text[i]]);
    115  1.1  pk 	    charrast = c->r;
    116  1.1  pk 	    if ( charrast != (struct raster*) 0 )
    117  1.1  pk 		{
    118  1.1  pk 		if ( x1 + c->homex < 0 || y1 + c->homey < 0 ||
    119  1.1  pk 		     x1 + c->homex + charrast->width > r->width ||
    120  1.1  pk 		     y1 + c->homey + charrast->height > r->height )
    121  1.1  pk 		    {
    122  1.1  pk 		    clip = 1;
    123  1.1  pk 		    break;
    124  1.1  pk 		    }
    125  1.1  pk 		}
    126  1.1  pk 	    }
    127  1.1  pk 	}
    128  1.1  pk 
    129  1.1  pk     /* Now display the text. */
    130  1.1  pk     for ( i = 0, x1 = x, y1 = y;
    131  1.1  pk 	  i < n;
    132  1.1  pk 	  ++i, x1 += c->nextx, y1 += c->nexty )
    133  1.1  pk 	{
    134  1.1  pk 	ch = text[i];
    135  1.1  pk 	c = &(rf->chars[ch]);
    136  1.1  pk 	charrast = c->r;
    137  1.1  pk 	if ( charrast != (struct raster*) 0 )
    138  1.1  pk 	    {
    139  1.1  pk 	    thisx = x1 + c->homex;
    140  1.1  pk 	    thisy = y1 + c->homey;
    141  1.1  pk 
    142  1.1  pk 	    phase = 0;
    143  1.1  pk #ifdef COLORFONT_CACHE
    144  1.1  pk 	    if ( r->depth == 8 )
    145  1.1  pk 		{
    146  1.1  pk 		/* Initialize color font cache if necessary. */
    147  1.1  pk 		if ( rf->cache == (struct raster_fontcache*) -1 )
    148  1.1  pk 		    {
    149  1.1  pk 		    int c;
    150  1.1  pk 
    151  1.1  pk 		    rf->cache = (struct raster_fontcache*)
    152  1.1  pk 			NEW( sizeof(struct raster_fontcache) );
    153  1.1  pk 		    if ( rf->cache != (struct raster_fontcache*) 0 )
    154  1.1  pk 			for ( c = 0; c < 256; ++c )
    155  1.1  pk 			    rf->cache->cr[c] = (struct raster*) 0;
    156  1.1  pk 		    }
    157  1.1  pk 
    158  1.1  pk 		if ( rf->cache != (struct raster_fontcache*) 0 )
    159  1.1  pk 		    {
    160  1.1  pk 		    int color;
    161  1.1  pk 		    struct raster* cr;
    162  1.1  pk 
    163  1.1  pk 		    color = RAS_GETCOLOR( rop );
    164  1.1  pk 		    cr = rf->cache->cr[ch];
    165  1.1  pk 		    /* Is this character cached yet? */
    166  1.1  pk 		    if ( cr != (struct raster*) 0 )
    167  1.1  pk 			{
    168  1.1  pk 			/* Yes, but is it the right color? */
    169  1.1  pk 			if ( rf->cache->color[ch] == color )
    170  1.1  pk 			    {
    171  1.1  pk 			    /* Yes - switch rasters. */
    172  1.1  pk 			    charrast = cr;
    173  1.1  pk 			    }
    174  1.1  pk 			else
    175  1.1  pk 			    {
    176  1.1  pk 			    /* No, re-draw it. */
    177  1.1  pk 			    if ( raster_op_noclip(
    178  1.1  pk 				 cr, 0, 0, charrast->width,
    179  1.1  pk 				 charrast->height, rop, charrast, 0, 0 ) == 0 )
    180  1.1  pk 				{
    181  1.1  pk 				rf->cache->color[ch] = color;
    182  1.1  pk 				charrast = cr;
    183  1.1  pk 				}
    184  1.1  pk 			    }
    185  1.1  pk 			}
    186  1.1  pk 		    else
    187  1.1  pk 			{
    188  1.1  pk 			/* It's not cached, so cache it. */
    189  1.1  pk 			cr = raster_alloc(
    190  1.1  pk 			    charrast->width, charrast->height, 8 );
    191  1.1  pk 			if ( cr != (struct raster*) 0 )
    192  1.1  pk 			    if ( raster_op_noclip(
    193  1.1  pk 				 cr, 0, 0, charrast->width, charrast->height,
    194  1.1  pk 				 rop, charrast, 0, 0 ) == 0 )
    195  1.1  pk 				{
    196  1.1  pk 				rf->cache->color[ch] = color;
    197  1.1  pk 				charrast = rf->cache->cr[ch] = cr;
    198  1.1  pk 				}
    199  1.1  pk 			}
    200  1.1  pk 		    }
    201  1.1  pk 		}
    202  1.1  pk #endif /*COLORFONT_CACHE*/
    203  1.1  pk 
    204  1.1  pk 	    if ( clip )
    205  1.1  pk 		{
    206  1.1  pk 		if ( raster_op(
    207  1.1  pk 			 r, thisx, thisy, charrast->width, charrast->height,
    208  1.1  pk 			 rop, charrast, phase, 0 ) < 0 )
    209  1.1  pk 		    return -1;
    210  1.1  pk 		}
    211  1.1  pk 	    else
    212  1.1  pk 		{
    213  1.1  pk 		if ( raster_op_noclip(
    214  1.1  pk 			 r, thisx, thisy, charrast->width, charrast->height,
    215  1.1  pk 			 rop, charrast, phase, 0 ) < 0 )
    216  1.1  pk 		    return -1;
    217  1.1  pk 		}
    218  1.1  pk 	    }
    219  1.1  pk 	}
    220  1.1  pk 
    221  1.1  pk     return 0;
    222  1.1  pk     }
    223  1.1  pk 
    224  1.1  pk #ifdef COLORFONT_CACHE
    225  1.1  pk /* Allocates a raster.  Returns (struct raster*) 0 on failure. */
    226  1.1  pk struct raster*
    227  1.1  pk raster_alloc( width, height, depth )
    228  1.1  pk     int width, height, depth;
    229  1.1  pk     {
    230  1.1  pk     struct raster* r;
    231  1.1  pk     int linelongs;
    232  1.1  pk 
    233  1.1  pk     if ( width <= 0 || height <= 0 || ( depth != 1 && depth != 8 ) )
    234  1.1  pk 	return (struct raster*) 0;
    235  1.1  pk     linelongs = ( ( width * depth + 31 ) >> 5 );
    236  1.1  pk     r = (struct raster*)
    237  1.1  pk 	NEW( sizeof(struct raster) + height * linelongs * sizeof(u_long));
    238  1.1  pk     if ( r == (struct raster*) 0 )
    239  1.1  pk 	return (struct raster*) 0;
    240  1.1  pk 
    241  1.1  pk     r->width = width;
    242  1.1  pk     r->height = height;
    243  1.1  pk     r->depth = depth;
    244  1.1  pk     r->linelongs = linelongs;
    245  1.1  pk     r->pixels = (u_long*) (r + 1);
    246  1.1  pk     r->data = (caddr_t) 0;
    247  1.1  pk     return r;
    248  1.1  pk     }
    249  1.1  pk #endif
    250