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