/*************************************************************************
Copyright(c) 2004-2006 Analog Devices, Inc. All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.

File Name: adi_graph_font.c 

Description - Part of 'adi_graph' library.  See readme file for details.

*********************************************************************************/

#include "adi_graph.h"
#include "../fonts/adi_graph_font.h"

void ADI_Text ( ADI_sGraphPrim *gp, const char *str )
{
	ADI_sFont *font = (ADI_sFont *) gp->font;
	int x_anchor = gp->x1;
	const int y_anchor = gp->y1;
	
	while ( *str )
	{
		const char *letter;
		if ( letter = font->letter[ (unsigned char) *str ] )
		{
			/* x,y points to the lower-left anchor of the character.  Because
			 * of the gutters, pixels may actually appear below or to the left
			 * of the anchor.  For example, a lower-case "j" extends below the
			 * "bottom" of the anchor point.  Sorta like the lined paper you
			 * learned to write with as a kid.
			 */
			 
			 const int lower_x = x_anchor + letter[ ADI_INDEX_BBX_START_X ];
			 const int upper_x = lower_x + letter[ ADI_INDEX_BBX_SIZE_X ] - 1;

			 const int lower_y = y_anchor + letter[ ADI_INDEX_BBX_START_Y ];
			 const int upper_y = lower_y + letter[ ADI_INDEX_BBX_SIZE_Y ] - 1 ;
			 
			 const char *bitmap = &letter[ ADI_INDEX_BITMAP ];
			 
			 /* Painting by row */
			 
			 int y;
			 int bit = 1<<7;
			 
			 for ( y = upper_y; y >= lower_y; --y )
			 {
			 	int x;
			 	for ( x = lower_x; x <= upper_x; ++x )
			 	{
				 	if ( *bitmap & bit )
				 	{
				 		gp->x1 = x;
				 		gp->y1 = y;
				 		ADI_Point ( gp );
				 	}
			 	
				 	if ( upper_x == x || 1 == bit )
			 		{
			 			bit = 1<<7;
			 			++bitmap;
			 		}
			 		else
			 		{
				 		bit >>= 1;
				 	}
			 	}
			 }
			 
		}
		
		++str;
		x_anchor += letter[ ADI_INDEX_DWIDTH ];
	}
}

void ADI_TextGetExtent ( ADI_sGraphPrim *gp, const char *str )
{
	ADI_sFont *font = (ADI_sFont *) gp->font;
	int width = 0;
	int height = 0;
	int descent = 0;
	
	while ( *str )
	{
		const char *letter;
		if ( letter = font->letter[ *str ] )
		{
			int test_height = letter[ ADI_INDEX_BBX_START_Y ] + letter[ ADI_INDEX_BBX_SIZE_Y ];
			int test_descent = letter[ ADI_INDEX_BBX_START_Y ];
			
			width += letter[ ADI_INDEX_DWIDTH ];
			
			if ( test_height > height )
				height = test_height;
				
			if ( test_descent < descent )
				descent = test_descent;
		}
		
		++str;
	}

	gp->x1 = width;
	gp->y1 = height;
	gp->y2 = descent;
}


