flightgear/3rdparty/fonts/fnt.cxx

342 lines
8.7 KiB
C++

/*
PLIB - A Suite of Portable Game Libraries
Copyright (C) 1998,2002 Steve Baker
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For further information visit http://plib.sourceforge.net
$Id: fnt.cxx 1939 2004-08-05 00:41:53Z puggles $
*/
#include "fntLocal.h"
#include <simgear/misc/sg_path.hxx>
#include <simgear/debug/logstream.hxx>
fntFont:: fntFont () {}
fntFont::~fntFont () {}
int fntTexFont::load ( const SGPath& path, GLenum mag, GLenum min )
{
if (path.extension() == "txf") {
return loadTXF ( path, mag, min ) ;
} else {
const auto ps = path.utf8Str();
fntSetError ( SG_WARN,
"fnt::load: Error - Unrecognised file format for '%s'", ps.c_str() ) ;
return FNT_FALSE ;
}
}
float fntTexFont::low_putch ( sgVec3 curpos, float pointsize,
float italic, int c )
{
unsigned int cc = (unsigned int) c ;
/* Auto case-convert if character is absent from font. */
//zhongjin
if ( ! exists [ cc ] )
{
//zhongjin
/*
if ( cc >= 'A' && cc <= 'Z' )
cc = cc - 'A' + 'a' ;
else
if ( cc >= 'a' && cc <= 'z' )
cc = cc - 'a' + 'A' ;
*/
//SG_LOG(SG_GUI,SG_ALERT, "low_putch 未找到字符 cc=" << cc);
if ( cc == ' ' )
{
curpos [ 0 ] += pointsize / 2.0f ;
return pointsize / 2.0f ;
}
}
/*
We might want to consider making some absent characters from
others (if they exist): lowercase 'l' could be made into digit '1'
or letter 'O' into digit '0'...or vice versa. We could also
make 'b', 'd', 'p' and 'q' by mirror-imaging - this would
save a little more texture memory in some fonts.
*/
if ( ! exists [ cc ] )
return 0.0f ;
glBegin ( GL_TRIANGLE_STRIP ) ;
glTexCoord2f ( t_left [cc], t_bot[cc] ) ;
glVertex3f ( curpos[0] + v_left [cc] * pointsize,
curpos[1] + v_bot [cc] * pointsize,
curpos[2] ) ;
glTexCoord2f ( t_left [cc], t_top[cc] ) ;
glVertex3f ( curpos[0] + (italic + v_left [cc]) * pointsize,
curpos[1] + v_top [cc] * pointsize,
curpos[2] ) ;
glTexCoord2f ( t_right[cc], t_bot[cc] ) ;
glVertex3f ( curpos[0] + v_right[cc] * pointsize,
curpos[1] + v_bot [cc] * pointsize,
curpos[2] ) ;
glTexCoord2f ( t_right[cc], t_top[cc] ) ;
glVertex3f ( curpos[0] + (italic + v_right[cc]) * pointsize,
curpos[1] + v_top [cc] * pointsize,
curpos[2] ) ;
glEnd () ;
float ww = ( gap + ( fixed_pitch ? width : widths[cc] ) ) * pointsize ;
curpos[0] += ww ;
return ww ;
}
void fntTexFont::setGlyph ( int c, float wid,
float tex_left, float tex_right,
float tex_bot , float tex_top ,
float vtx_left, float vtx_right,
float vtx_bot , float vtx_top )
{
unsigned int cc = (unsigned int) c ;
exists[cc] = FNT_TRUE ;
widths[cc] = wid;
t_left[cc] = tex_left ; t_right[cc] = tex_right ;
t_bot [cc] = tex_bot ; t_top [cc] = tex_top ;
v_left[cc] = vtx_left ; v_right[cc] = vtx_right ;
v_bot [cc] = vtx_bot ; v_top [cc] = vtx_top ;
}
int fntTexFont::getGlyph ( char c, float* wid,
float *tex_left, float *tex_right,
float *tex_bot , float *tex_top ,
float *vtx_left, float *vtx_right,
float *vtx_bot , float *vtx_top )
{
//zhongjin
//SG_LOG(SG_GUI,SG_ALERT, "getGlyph " << c);
unsigned int cc = (unsigned char) c ;
if ( ! exists[cc] ) return FNT_FALSE ;
if ( wid != NULL ) *wid = widths [cc] ;
if ( tex_left != NULL ) *tex_left = t_left [cc] ;
if ( tex_right != NULL ) *tex_right = t_right[cc] ;
if ( tex_bot != NULL ) *tex_bot = t_bot [cc] ;
if ( tex_top != NULL ) *tex_top = t_top [cc] ;
if ( vtx_left != NULL ) *vtx_left = v_left [cc] ;
if ( vtx_right != NULL ) *vtx_right = v_right[cc] ;
if ( vtx_bot != NULL ) *vtx_bot = v_bot [cc] ;
if ( vtx_top != NULL ) *vtx_top = v_top [cc] ;
return FNT_TRUE ;
}
void fntTexFont::getBBox ( const char *s,
float pointsize, float italic,
float *left, float *right,
float *bot , float *top )
{
float h_pos = 0.0f ;
float v_pos = 0.0f ;
float l, r, b, t ;
//zhongjin
//SG_LOG(SG_GUI,SG_ALERT, "getBBox " << s);
l = r = b = t = 0.0f ;
while ( *s != '\0' )
{
if ( *s == '\n' )
{
h_pos = 0.0f ;
v_pos -= 1.333f ;
s++ ;
continue ;
}
unsigned int cc = (unsigned char) *(s++) ;
if ((cc & 0x80)==0) {
//SG_LOG(SG_GUI,SG_ALERT, "ASSIC cc=" << cc);
}
if ( (cc & 0XE0) == 0XE0) {
//SG_LOG(SG_GUI,SG_ALERT, "UTF83 cc=" << cc);
int x = (cc & 0X0F) << 4;
//SG_LOG(SG_GUI,SG_ALERT, "UTF8 cc<<4 " << x);
cc = (unsigned char) *(s++) ;
//SG_LOG(SG_GUI,SG_ALERT, "UTF82 cc=" << cc);
x= x + ((cc & 0x3C) >>2 ) ;
int y = (cc & 0x03) << 6 ;
cc = (unsigned char) *(s++) ;
//SG_LOG(SG_GUI,SG_ALERT, "UTF81 cc=" << cc);
y= y + (cc & 0x3F);
//SG_LOG(SG_GUI,SG_ALERT, "x=" << x << ",y=" << y);
cc = x * 256 + y;
//SG_LOG(SG_GUI,SG_ALERT, "GBK cc=" << cc);
}
if ( ! exists [ cc ] )
{
/*
if ( cc >= 'A' && cc <= 'Z' )
cc = cc - 'A' + 'a' ;
else
if ( cc >= 'a' && cc <= 'z' )
cc = cc - 'a' + 'A' ;
*/
if ( cc == ' ' )
{
r += 0.5f ;
h_pos += 0.5f ;
continue ;
}
}
if ( ! exists [ cc ] ) {
//SG_LOG(SG_GUI,SG_ALERT, "getBBox 未找到字符 cc=" << cc);
continue ;
}
if ( italic >= 0 )
{
if ( l > h_pos + v_left [cc] ) l = h_pos + v_left [cc] ;
if ( r < gap + h_pos + v_right[cc]+italic ) r = gap + h_pos + v_right[cc] + italic ;
}
else
{
if ( l > h_pos + v_left [cc]+italic ) l = h_pos + v_left [cc] + italic ;
if ( r < gap + h_pos + v_right[cc] ) r = gap + h_pos + v_right[cc] ;
}
if ( b > v_pos + v_bot [cc] ) b = v_pos + v_bot [cc] ;
if ( t < v_pos + v_top [cc] ) t = v_pos + v_top [cc] ;
h_pos += gap + ( fixed_pitch ? width : widths[cc] ) ;
}
//zhongjin
//SG_LOG(SG_GUI,SG_ALERT, "......................");
if ( left != NULL ) *left = l * pointsize ;
if ( right != NULL ) *right = r * pointsize ;
if ( top != NULL ) *top = t * pointsize ;
if ( bot != NULL ) *bot = b * pointsize ;
}
void fntTexFont::puts ( sgVec3 curpos, float pointsize, float italic, const char *s )
{
SGfloat origx = curpos[0] ;
if ( ! bound )
bind_texture () ;
//zhongjin
//SG_LOG(SG_GUI,SG_ALERT, "puts " << s);
while ( *s != '\0' )
{
unsigned int cc = (unsigned char) *(s++) ;
if (*s == '\n')
{
curpos[0] = origx ;
curpos[1] -= pointsize ;
}
else
if ( (cc & 0XE0) == 0XE0) {
//SG_LOG(SG_GUI,SG_ALERT, "UTF83 cc=" << cc);
int x = (cc & 0X0F) << 4;
//SG_LOG(SG_GUI,SG_ALERT, "UTF8 cc<<4 " << x);
cc = (unsigned char) *(s++) ;
//SG_LOG(SG_GUI,SG_ALERT, "UTF82 cc=" << cc);
x= x + ((cc & 0x3C) >>2 ) ;
int y = (cc & 0x03) << 6 ;
cc = (unsigned char) *(s++) ;
//SG_LOG(SG_GUI,SG_ALERT, "UTF81 cc=" << cc);
y= y + (cc & 0x3F);
//SG_LOG(SG_GUI,SG_ALERT, "x=" << x << ",y=" << y);
cc = x * 256 + y;
//SG_LOG(SG_GUI,SG_ALERT, "GBK cc=" << cc);
}
//SG_LOG(SG_GUI,SG_ALERT, "puts->low_putch " << cc);
//low_putch ( curpos, pointsize, italic, *s ) ;
low_putch ( curpos, pointsize, italic, cc ) ;
}
}
void fntInit ()
{
/* Empty right now */
}
void fntSetError(int level, const char* format, ...)
{
const int bufferLength = 2048;
char buffer[2048];
va_list args;
va_start (args, format);
vsnprintf(buffer,bufferLength,format, args);
va_end (args);
SG_LOG(SG_GUI, static_cast<sgDebugPriority>(level), buffer);
}