1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* text draw truetypefont
*
* 2004/01/30
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "config.h"
#ifdef HAVE_ICONV_H
#include <iconv.h>
#endif
#ifdef HAVE_FT2BUILD_H
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
#include "driver.h"
#include "driverlib.h"
/*#define DEBUG_LOG(S) {FILE *fp = fopen("debug.TXT","a");fputs(S,fp);fclose(fp);}*/
/*#define DEBUG_LOG_INT(D) {FILE *fp = fopen("debug.TXT","a");fprintf(fp,"%d",D);fclose(fp);}*/
/*#define DEBUG_LOG_DOUBLE(D) {FILE *fp = fopen("debug.TXT","a");fprintf(fp,"%f",D);fclose(fp);}*/
#ifdef HAVE_FT2BUILD_H
static int convert_str(char*,char*,unsigned char**);
static void release_convert_str(unsigned char*);
static int setMatrix(FT_Matrix*,double);
static int draw_text(FT_Face,FT_Vector*,FT_Matrix*,unsigned char*,int,int);
static void draw_bitmap(FT_Bitmap*,FT_Int,FT_Int);
static void set_text_box(FT_Bitmap*,FT_Int,FT_Int);
#endif
static int fdont_draw = 0;
static int ft, fb, fl, fr;
static int drawMain(int x,int y,double text_size_x,double text_size_y,double text_rotation,char *string)
{
#ifdef HAVE_FT2BUILD_H
FT_Library library;
FT_Face face;
FT_Matrix matrix;
/*FT_UInt glyph_index;*/
FT_Vector pen;
FT_Error ans;
char* filename;
char* charset;
unsigned char* out;
int outlen;
/* get file name */
filename = getFreeTypeName();
charset = getCharset();
/* set freetype */
ans = FT_Init_FreeType(&library);
if(ans) {
/* DEBUG_LOG("Text3 error: ft init\n"); */
return -1;
}
ans = FT_New_Face(library,filename,0,&face);
if(ans==FT_Err_Unknown_File_Format) {
/* DEBUG_LOG("Text3 error: ft new face 1\n"); */
FT_Done_FreeType(library);
return -1;
}else if(ans) {
/* DEBUG_LOG("Text3 error: ft new face 2\n"); */
FT_Done_FreeType(library);
return -1;
}
/* ans = FT_Set_Pixel_Sizes(face,10,10); */
/* ans = FT_Set_Char_Size(face,text_size_x*64,text_size_y*64,0,0); */
/* ans = FT_Set_Char_Size(face,10*64,0,72,0); */
/* ans = FT_Set_Char_Size(face,text_size_x*64,text_size_y*64,72,72); */
ans = FT_Set_Char_Size(face,text_size_x*64,text_size_y*64,100,100);
/*
ans = FT_Set_Pixel_Sizes(
face,
0,
(int)text_size_y );
*/
if(ans) {
/* DEBUG_LOG("Text3 error: ft set size\n"); */
FT_Done_Face(face);
FT_Done_FreeType(library);
return -1;
}
/* init point */
pen.x = x*64;
/* pen.y = 0; */
pen.y = (screen_bottom-y)*64;
/* convert string to:shift-jis from:charset */
outlen = convert_str(charset,string,&out);
/* set matrix */
setMatrix(&matrix,text_rotation);
/* draw */
draw_text(face,&pen,&matrix,out,outlen,0);
/* release */
release_convert_str(out);
/* FT_done */
FT_Done_Face(face);
FT_Done_FreeType(library);
#endif
return 0;
}
#ifdef HAVE_FT2BUILD_H
static int setMatrix(FT_Matrix* matrix,double rotation)
{
matrix->xx = (FT_Fixed)( cos(rotation)*0x10000);
matrix->xy = (FT_Fixed)(-sin(rotation)*0x10000);
matrix->yx = (FT_Fixed)( sin(rotation)*0x10000);
matrix->yy = (FT_Fixed)( cos(rotation)*0x10000);
return 0;
}
static int convert_str(char* from,char* in,unsigned char** out)
{
iconv_t cd;
size_t ret;
int len = 0;
int i = 0;
int res = 0;
unsigned char* p1;
unsigned char* p2;
len = strlen(in);
res = 2*(len+1);
/* res = 4*(len+1); */
*out = (unsigned char*)malloc(res);
memset(*out,res,0);
p1 = in;
p2 = *out;
i = res;
if( (cd = iconv_open("UCS-2BE",from)) < 0 ) {
/* if( (cd = iconv_open("UCS-4",from)) < 0 ) { */
/* if( (cd = iconv_open("UTF-8",from)) < 0 ) { */
return -1;
}
ret = iconv(cd,(char**)&p1,&len,(char**)&p2,&i);
/*
if(ret == -1){
if(errno == E2BIG){
{DEBUG_LOG("E2BIG\n")}
}
if(errno == EILSEQ){
{DEBUG_LOG("EILSEQ\n")}
}
if(errno == EINVAL){
{DEBUG_LOG("EILSEQ\n")}
}
}
*/
iconv_close(cd);
res -= i;
return res;
}
static void release_convert_str(unsigned char* out)
{
free(out);
}
static int draw_text(FT_Face face,FT_Vector* pen,FT_Matrix* matrix,unsigned char* out,int len,int color)
{
int i=0;
FT_ULong ch;
FT_Error ans;
FT_GlyphSlot slot = face->glyph;
for(i=0;i<len;i+=2) {
ch = (out[i]<<8)|out[i+1];
if(ch == 10) continue;
/* transform */
FT_Set_Transform(face,matrix,pen);
/* get glyph image */
ans = FT_Load_Char(face,ch,FT_LOAD_NO_BITMAP);
if(ans) continue;
ans = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
if ( ans ) continue;
/* draw bitmap */
if(fdont_draw==0) {
draw_bitmap(&slot->bitmap,slot->bitmap_left,screen_bottom-slot->bitmap_top);
}else{
set_text_box(&slot->bitmap,slot->bitmap_left,screen_bottom-slot->bitmap_top);
}
/* increment pen position */
pen->x += slot->advance.x;
pen->y += slot->advance.y;
}
return 0;
}
static void set_text_box(FT_Bitmap* bitmap,FT_Int x,FT_Int y)
{
FT_Int xMax = x + bitmap->width;
FT_Int yMax = y + bitmap->rows;
if( (x == xMax) || (y == yMax) ) return;
if(x<fl) fl = x;
if(xMax>fr) fr = xMax;
if(y<ft) ft = y;
if(yMax>fb) fb = yMax;
return;
}
static void draw_bitmap(FT_Bitmap* bitmap,FT_Int x,FT_Int y)
{
FT_Int i,j,p,q;
unsigned char color;
FT_Int xMax = x + bitmap->width;
FT_Int yMax = y + bitmap->rows;
for(i=x,p=0;i<xMax;i++,p++) {
for(j=y,q=0;j<yMax;j++,q++) {
color = bitmap->buffer[q*bitmap->width+p];
if(color>128) {
draw_point(i,j);
}else{
}
}
}
return;
}
#endif
int soft_text_freetype(int x,int y,double text_size_x,double text_size_y,double text_rotation,char *string)
{
text_size_x *= 25.0;
text_size_y *= 25.0;
drawMain(x,y,text_size_x,text_size_y,text_rotation,string);
return 0;
}
int soft_text_ext_freetype(int x,int y,double text_size_x,double text_size_y,double text_rotation,char *string)
{
fdont_draw = 1;
text_size_x *= 25.0;
text_size_y *= 25.0;
ft = 999999;
fb = 0;
fl = 999999;
fr = 0;
/* drawMain(x,y,text_size_x,text_size_y,text_rotation,string); */
/* drawMain(0,0,text_size_x,text_size_y,text_rotation,string); */
drawMain(0,y,text_size_x,text_size_y,text_rotation,string);
/* ft += y; */
/* fb += y; */
fl += x;
fr += x;
fdont_draw = 0;
return 0;
}
int get_text_ext_freetype(int *top,int *bot,int *left,int *rite)
{
*top = ft;
*bot = fb;
*left = fl;
*rite = fr;
return 0;
}
|