⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hz.c

📁 nds上的电子书软件
💻 C
字号:
/*
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *
 *	Copyright (c) 2006 hulihutu <huozhiniao@sina.com>
 *
 * hz.c
 *
 * This is a lib to display GBK Chinese and English text on 
 * a screen for NDS.
 * Need font files : ASC12, ASC16, GBK12, GBK16
 *
 * ChangeLog:
 *
 * 2006.4.5
 *   Clear up function names
 *   change font files searching to by name
 * 
 * 2006.3.30
 *   add function OutputHzTextExt()
 *   change char boundary check, add LR judgement, fix bug 
 *
 * 2006.3.29
 *   create original function
 */

#include <PA9.h>
#include "hz.h"
 
//max amount of chars displayed on a srceen
#define MAXCHAR 1000

static u8 *eAFontBuffer = NULL, *cAFontBuffer = NULL;
static u8 *eBFontBuffer = NULL, *cBFontBuffer = NULL;
static u8 *eFontBuffer = NULL, *cFontBuffer = NULL;

// Function: load font file
extern bool LoadGbkFontA(char *eFontName, char *eFontExt, char *cFontName,
  char *cFontExt)
{
	s32 idx; 
	//Search and load English font
	idx = PA_FSGetFile(0, eFontName, eFontExt);
	if(idx != -1)
	{ 
		eAFontBuffer = (u8 *)PA_PAFSFile(idx);
	}
	else
	{
		return false;
	}
  
  //load Chinese font
	idx = PA_FSGetFile(0, cFontName, cFontExt);
	if(idx != -1)
	{ 
		cAFontBuffer = (u8 *)PA_PAFSFile(idx);
	}
	else
	{
		return false;
	}
	
	return true;
}

// Function: load font file
extern bool LoadGbkFontB(char *eFontName, char *eFontExt, char *cFontName,
  char *cFontExt)
{
	s32 idx; 
	//Search and load English font
	idx = PA_FSGetFile(0, eFontName, eFontExt);
	if(idx != -1)
	{ 
		eBFontBuffer = (u8 *)PA_PAFSFile(idx);
	}
	else
	{
		return false;
	}
  
  //load Chinese font
	idx = PA_FSGetFile(0, cFontName, cFontExt);
	if(idx != -1)
	{ 
		cBFontBuffer = (u8 *)PA_PAFSFile(idx);
	}
	else
	{
		return false;
	}
	
	return true;
}

// Function: draw Chinese Text
extern u32 OutputGbkTextExt(bool screen, u16 basex, u16 basey, u16 maxx, u16 maxy, \
	u16 color, const char *text, u32 limit, int fontsize, u16 nextlinex)
{
	u8 ch;
	byte * ccur;
	u16 x = basex;
	u16 y = basey;
	int i,j,k,qu,b,wei,count = 0;
	int cfont_size = fontsize * fontsize / 8;	//space of a char in font file
	long offset;   //offset of a char in font file
	
	if(maxx > SCREEN_WIDTH)		maxx = SCREEN_WIDTH;
	if(maxy > SCREEN_HEIGHT)	maxy = SCREEN_HEIGHT;
	
	while((*text) && limit >0)
	{
		if(x > maxx - ((*text > 0x80) ? fontsize : fontsize / 2))	//check if x and y exceed max
		{
		   x = nextlinex;
		   y += fontsize;
		   if(y > maxy)	return count;
		}		
		limit--;
		count++;		
		
		if(*text > 0x80)	// is a Chinese char
		{
			qu = *(text++) - 0x80;	//qu ma 
		 	wei = *(text++) - 0x40;	//wei ma
		 	offset = (0xbf * (qu - 1) + wei) * cfont_size;

		 	ccur = cFontBuffer + offset;	//point to the char's font info
		 	switch(fontsize)	// draw a char
		 	{
		   case 16 :
				for(i = 0; i < 16; i++)
		 			for(j = 0; j < 2; j++)
		 			{
		 				ch = *ccur++;
		 				for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			PA_Put16bitPixel(screen, x + j * 8 + k, y + i,color);
		      	}
		  		break;
		 	case 12 :
		 		for(i = 0; i < 12; i++)
		 		{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			PA_Put16bitPixel(screen, x + k, y + i,color);
		      		
		 			ch = *ccur++;
		 			for(b = 0x80; b > 8; b >>= 1, k++)
		      		if(ch & b)
		      			PA_Put16bitPixel(screen, x + k, y + i,color);
		      	i++;
		      
		 			for(k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			PA_Put16bitPixel(screen, x + k, y + i,color);
		      
		      	ch = *ccur++;
		 			for(b = 0x80; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			PA_Put16bitPixel(screen, x + k, y + i,color);
		    	}
		    	break;		
			}	//end of switch
			x += fontsize;
			limit--;
			count++;
		}		
		else if(*text > 0x1F)
		{
		   if(fontsize == 12)
      		offset = (*(text++) - 32) * fontsize; 
      	else
      		offset = (*(text++)) * fontsize;
      	
      	ccur = eFontBuffer + offset;	//point to the char's font info
		 	switch(fontsize)	// draw a char
		 	{
		   case 16 :
				for(i = 0; i < 16; i++)
				{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      	if(ch & b)
		      		PA_Put16bitPixel(screen, x + k, y + i,color);
		      }
		  		break;
		 	case 12 :
		 		for(i = 0; i < 12; i++)
				{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 2; b >>= 1, k++)
		      	if(ch & b)
		      		PA_Put16bitPixel(screen, x + k, y + i,color);
		      }
		  		break;
			}	//end of switch
			x += fontsize / 2;
		}
		else if(*text == 0x0a)	//LF('\n')
		{
		   text++;
			x = nextlinex;
		   y += fontsize;
		   if(y > maxy)	return count;
		}
		else 
		{
			text++;
			x += fontsize / 2;
		}	//end of if				
	}	//end of while
	
	return count;
}

// Function: draw Chinese Text
extern u32 OutputGbkTextExtV(bool screen, u16 basex, u16 basey, u16 maxx, u16 maxy, \
	u16 color, const char *text, u32 limit, int fontsize, u16 nextlinex)
{
	u8 ch;
	byte * ccur;
	int x = basex-fontsize;
	int y = basey;
	int i,j,k,qu,b,wei,count = 0;
	int cfont_size = fontsize * fontsize / 8;	//space of a char in font file
	long offset;   //offset of a char in font file
	
	if(maxx > SCREEN_WIDTH)		maxx = SCREEN_WIDTH;
	if(maxy > SCREEN_HEIGHT)	maxy = SCREEN_HEIGHT;
	
	while((*text) && limit >0)
	{
		if(y > maxy - ((*text > 0x80) ? fontsize : fontsize / 2))	//check if x and y exceed max
		{
		   y = nextlinex;
		   x -= (fontsize+2);
		   if(x < 16/*SCREEN_WIDTH-maxx*/)	return count;
		}		
		limit--;
		count++;		
		
		if(*text > 0x80)	// is a Chinese char
		{
			qu = *(text++) - 0x80;	//qu ma 
		 	wei = *(text++) - 0x40;	//wei ma
		 	offset = (0xbf * (qu - 1) + wei) * cfont_size;

		 	ccur = cFontBuffer + offset;	//point to the char's font info
		 	switch(fontsize)	// draw a char
		 	{
		   case 16 :
				for(i = 0; i < 16; i++)
		 			for(j = 0; j < 2; j++)
		 			{
		 				ch = *ccur++;
		 				for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			//PA_Put16bitPixel(screen, x + j * 8 + k, y + i,color);
						PA_Put16bitPixel(screen, x + 15-i, y+j*8+k,color);
		      	}
		  		break;
		 	case 12 :
		 		for(i = 0; i < 12; i++)
		 		{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			//PA_Put16bitPixel(screen, x + k, y + i,color);
						PA_Put16bitPixel(screen, x +11-i , y + k,color);
		      		
		 			ch = *ccur++;
		 			for(b = 0x80; b > 8; b >>= 1, k++)
		      		if(ch & b)
		      			//PA_Put16bitPixel(screen, x + k, y + i,color);
						PA_Put16bitPixel(screen, x +11-i , y + k,color);
		      	i++;
		      
		 			for(k = 0; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			//PA_Put16bitPixel(screen, x + k, y + i,color);
						PA_Put16bitPixel(screen, x +11-i , y + k,color);
		      
		      	ch = *ccur++;
		 			for(b = 0x80; b > 0; b >>= 1, k++)
		      		if(ch & b)
		      			//PA_Put16bitPixel(screen, x + k, y + i,color);
						PA_Put16bitPixel(screen, x +11-i , y + k,color);
		    	}
		    	break;		
			}	//end of switch
			y += fontsize;
			limit--;
			count++;
		}		
		else if(*text > 0x1F)
		{
		   if(fontsize == 12)
      		offset = (*(text++) - 32) * fontsize; 
      	else
      		offset = (*(text++)) * fontsize;
      	
      	ccur = eFontBuffer + offset;	//point to the char's font info
		 	switch(fontsize)	// draw a char
		 	{
		   case 16 :
				for(i = 0; i < 16; i++)
				{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 0; b >>= 1, k++)
		      	if(ch & b)
		      		//PA_Put16bitPixel(screen, x + k, y + i,color);
					PA_Put16bitPixel(screen, x +15-i , y + k,color);
		      }
		  		break;
		 	case 12 :
		 		for(i = 0; i < 12; i++)
				{
		 			ch = *ccur++;
		 			for(b = 0x80, k = 0; b > 2; b >>= 1, k++)
		      	if(ch & b)
		      		//PA_Put16bitPixel(screen, x + k, y + i,color);
					PA_Put16bitPixel(screen, x +11-i , y + k,color);
		      }
		  		break;
			}	//end of switch
			y += fontsize / 2;
		}
		else if(*text == 0x0a)	//LF('\n')
		{
		   text++;
			y = nextlinex;
		   x-= (fontsize+2);
		   if(x <16/*SCREEN_WIDTH-maxx*/)	return count;
		}
		else 
		{
			text++;
			y += fontsize / 2;
		}	//end of if				
	}	//end of while
	
	return count;
}

// Function: draw Chinese Text
extern void OutputGbkText(bool screen, u16 x, u16 y, u16 color, const char *text, int fontsize)
{
	
	OutputGbkTextExt(screen, x, y, SCREEN_WIDTH, SCREEN_HEIGHT, color, text, MAXCHAR, fontsize, 0); 
}

// Function: draw Chinese Text
extern void OutputGbkTextV(bool screen, u16 x, u16 y, u16 color, const char *text, int fontsize)
{
	OutputGbkTextExtV(screen, x, y, SCREEN_WIDTH, SCREEN_HEIGHT, color, text, MAXCHAR, fontsize, 0); 
}

extern void SetFontA()
{
	eFontBuffer=eAFontBuffer;
	cFontBuffer=cAFontBuffer;
}

extern void SetFontB()
{
	eFontBuffer=eBFontBuffer;
	cFontBuffer=cBFontBuffer;
}		

// Function: draw Chinese Text in box
extern u32 OutputGbkBoxText(bool screen, u16 basex, u16 basey, u16 maxx, u16 maxy, \
	u16 color, const char *text, u32 limit, int fontsize)
{
   return OutputGbkTextExt(screen, basex, basey, maxx, maxy, color, text, limit, fontsize, basex);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -