Home | History | Annotate | Line # | Download | only in rcons
      1  1.10      matt /*	$NetBSD: raster_text.c,v 1.10 2012/01/31 04:28:03 matt 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.10      matt __KERNEL_RCSID(0, "$NetBSD: raster_text.c,v 1.10 2012/01/31 04:28:03 matt 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.10      matt raster_text(
     65  1.10      matt     struct raster* r,
     66  1.10      matt     int x,
     67  1.10      matt     int y,
     68  1.10      matt     int rop,
     69  1.10      matt     struct raster_font* rf,
     70  1.10      matt     unsigned 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.10      matt raster_textn(
     78  1.10      matt     struct raster* r,
     79  1.10      matt     int x,
     80  1.10      matt     int y,
     81  1.10      matt     int rop,
     82  1.10      matt     struct raster_font* rf,
     83  1.10      matt     unsigned char* text,
     84  1.10      matt     int n)
     85   1.1        pk     {
     86   1.1        pk     int clip;
     87   1.1        pk     int x1, y1;
     88   1.1        pk     struct raster_char* c;
     89   1.1        pk     struct raster* charrast;
     90   1.1        pk     int i;
     91   1.4  augustss     unsigned char ch;
     92   1.1        pk     int thisx, thisy;
     93   1.1        pk     int phase;
     94   1.1        pk 
     95   1.1        pk     /* Check whether we can avoid clipping. */
     96   1.1        pk     clip = 0;
     97   1.1        pk     if ( rf->flags & RASFONT_FIXEDWIDTH &&
     98   1.1        pk 	 rf->flags & RASFONT_NOVERTICALMOVEMENT )
     99   1.1        pk 	{
    100   1.1        pk 	/* This font is well-behaved, we can compute the extent cheaply. */
    101   1.1        pk 	c = &(rf->chars['@']);
    102   1.1        pk 	charrast = c->r;
    103   1.1        pk 	if ( x + c->homex < 0 || y + c->homey < 0 ||
    104   1.1        pk 	     x + c->homex + n * c->nextx > r->width ||
    105   1.1        pk 	     y + c->homey + charrast->height > r->height )
    106   1.1        pk 	    clip = 1;
    107   1.1        pk 	}
    108   1.1        pk     else
    109   1.1        pk 	{
    110   1.1        pk 	/* Got to step through the string to compute the extent. */
    111   1.1        pk 	for ( i = 0, x1 = x, y1 = y;
    112   1.1        pk 	      i < n;
    113   1.1        pk 	      ++i, x1 += c->nextx, y1 += c->nexty )
    114   1.1        pk 	    {
    115   1.1        pk 	    c = &(rf->chars[text[i]]);
    116   1.1        pk 	    charrast = c->r;
    117   1.1        pk 	    if ( charrast != (struct raster*) 0 )
    118   1.1        pk 		{
    119   1.1        pk 		if ( x1 + c->homex < 0 || y1 + c->homey < 0 ||
    120   1.1        pk 		     x1 + c->homex + charrast->width > r->width ||
    121   1.1        pk 		     y1 + c->homey + charrast->height > r->height )
    122   1.1        pk 		    {
    123   1.1        pk 		    clip = 1;
    124   1.1        pk 		    break;
    125   1.1        pk 		    }
    126   1.1        pk 		}
    127   1.1        pk 	    }
    128   1.1        pk 	}
    129   1.1        pk 
    130   1.1        pk     /* Now display the text. */
    131   1.1        pk     for ( i = 0, x1 = x, y1 = y;
    132   1.1        pk 	  i < n;
    133   1.1        pk 	  ++i, x1 += c->nextx, y1 += c->nexty )
    134   1.1        pk 	{
    135   1.1        pk 	ch = text[i];
    136   1.1        pk 	c = &(rf->chars[ch]);
    137   1.1        pk 	charrast = c->r;
    138   1.1        pk 	if ( charrast != (struct raster*) 0 )
    139   1.1        pk 	    {
    140   1.1        pk 	    thisx = x1 + c->homex;
    141   1.1        pk 	    thisy = y1 + c->homey;
    142   1.1        pk 
    143   1.1        pk 	    phase = 0;
    144   1.1        pk #ifdef COLORFONT_CACHE
    145   1.1        pk 	    if ( r->depth == 8 )
    146   1.1        pk 		{
    147   1.1        pk 		/* Initialize color font cache if necessary. */
    148   1.1        pk 		if ( rf->cache == (struct raster_fontcache*) -1 )
    149   1.1        pk 		    {
    150   1.1        pk 		    int c;
    151   1.1        pk 
    152   1.1        pk 		    rf->cache = (struct raster_fontcache*)
    153   1.1        pk 			NEW( sizeof(struct raster_fontcache) );
    154   1.1        pk 		    if ( rf->cache != (struct raster_fontcache*) 0 )
    155   1.1        pk 			for ( c = 0; c < 256; ++c )
    156   1.1        pk 			    rf->cache->cr[c] = (struct raster*) 0;
    157   1.1        pk 		    }
    158   1.1        pk 
    159   1.1        pk 		if ( rf->cache != (struct raster_fontcache*) 0 )
    160   1.1        pk 		    {
    161   1.1        pk 		    int color;
    162   1.1        pk 		    struct raster* cr;
    163   1.1        pk 
    164   1.1        pk 		    color = RAS_GETCOLOR( rop );
    165   1.1        pk 		    cr = rf->cache->cr[ch];
    166   1.1        pk 		    /* Is this character cached yet? */
    167   1.1        pk 		    if ( cr != (struct raster*) 0 )
    168   1.1        pk 			{
    169   1.1        pk 			/* Yes, but is it the right color? */
    170   1.1        pk 			if ( rf->cache->color[ch] == color )
    171   1.1        pk 			    {
    172   1.1        pk 			    /* Yes - switch rasters. */
    173   1.1        pk 			    charrast = cr;
    174   1.1        pk 			    }
    175   1.1        pk 			else
    176   1.1        pk 			    {
    177   1.1        pk 			    /* No, re-draw it. */
    178   1.1        pk 			    if ( raster_op_noclip(
    179   1.1        pk 				 cr, 0, 0, charrast->width,
    180   1.1        pk 				 charrast->height, rop, charrast, 0, 0 ) == 0 )
    181   1.1        pk 				{
    182   1.1        pk 				rf->cache->color[ch] = color;
    183   1.1        pk 				charrast = cr;
    184   1.1        pk 				}
    185   1.1        pk 			    }
    186   1.1        pk 			}
    187   1.1        pk 		    else
    188   1.1        pk 			{
    189   1.1        pk 			/* It's not cached, so cache it. */
    190   1.1        pk 			cr = raster_alloc(
    191   1.1        pk 			    charrast->width, charrast->height, 8 );
    192   1.1        pk 			if ( cr != (struct raster*) 0 )
    193   1.1        pk 			    if ( raster_op_noclip(
    194   1.1        pk 				 cr, 0, 0, charrast->width, charrast->height,
    195   1.1        pk 				 rop, charrast, 0, 0 ) == 0 )
    196   1.1        pk 				{
    197   1.1        pk 				rf->cache->color[ch] = color;
    198   1.1        pk 				charrast = rf->cache->cr[ch] = cr;
    199   1.1        pk 				}
    200   1.1        pk 			}
    201   1.1        pk 		    }
    202   1.1        pk 		}
    203   1.1        pk #endif /*COLORFONT_CACHE*/
    204   1.1        pk 
    205   1.1        pk 	    if ( clip )
    206   1.1        pk 		{
    207   1.1        pk 		if ( raster_op(
    208   1.1        pk 			 r, thisx, thisy, charrast->width, charrast->height,
    209   1.1        pk 			 rop, charrast, phase, 0 ) < 0 )
    210   1.1        pk 		    return -1;
    211   1.1        pk 		}
    212   1.1        pk 	    else
    213   1.1        pk 		{
    214   1.1        pk 		if ( raster_op_noclip(
    215   1.1        pk 			 r, thisx, thisy, charrast->width, charrast->height,
    216   1.1        pk 			 rop, charrast, phase, 0 ) < 0 )
    217   1.1        pk 		    return -1;
    218   1.1        pk 		}
    219   1.1        pk 	    }
    220   1.1        pk 	}
    221   1.1        pk 
    222   1.1        pk     return 0;
    223   1.1        pk     }
    224   1.1        pk 
    225   1.1        pk #ifdef COLORFONT_CACHE
    226   1.1        pk /* Allocates a raster.  Returns (struct raster*) 0 on failure. */
    227   1.1        pk struct raster*
    228  1.10      matt raster_alloc(
    229  1.10      matt     int width,
    230  1.10      matt     int height,
    231  1.10      matt     int 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.9  christos     r->data = (void *) 0;
    250   1.1        pk     return r;
    251   1.1        pk     }
    252   1.1        pk #endif
    253