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