| 1 | /* |
| 2 | PLIB - A Suite of Portable Game Libraries |
| 3 | Copyright (C) 1998,2002 Steve Baker |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Library General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Library General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | For further information visit http://plib.sourceforge.net |
| 20 | |
| 21 | $Id: fnt.cxx 1939 2004-08-05 00:41:53Z puggles $ |
| 22 | */ |
| 23 | |
| 24 | #include "fntLocal.h" |
| 25 | |
| 26 | #include <simgear/misc/sg_path.hxx> |
| 27 | #include <simgear/debug/logstream.hxx> |
| 28 | |
| 29 | fntFont:: fntFont () {} |
| 30 | fntFont::~fntFont () {} |
| 31 | |
| 32 | int fntTexFont::load ( const SGPath& path, GLenum mag, GLenum min ) |
| 33 | { |
| 34 | if (path.extension() == "txf") { |
| 35 | return loadTXF ( path, mag, min ) ; |
| 36 | } else { |
| 37 | const auto ps = path.utf8Str(); |
| 38 | fntSetError ( SG_WARN, |
| 39 | "fnt::load: Error - Unrecognised file format for '%s'", ps.c_str() ) ; |
| 40 | return FNT_FALSE ; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | float fntTexFont::low_putch ( sgVec3 curpos, float pointsize, |
| 45 | float italic, char c ) |
| 46 | { |
| 47 | unsigned int cc = (unsigned char) c ; |
| 48 | |
| 49 | /* Auto case-convert if character is absent from font. */ |
| 50 | |
| 51 | if ( ! exists [ cc ] ) |
| 52 | { |
| 53 | if ( cc >= 'A' && cc <= 'Z' ) |
| 54 | cc = cc - 'A' + 'a' ; |
| 55 | else |
| 56 | if ( cc >= 'a' && cc <= 'z' ) |
| 57 | cc = cc - 'a' + 'A' ; |
| 58 | |
| 59 | if ( cc == ' ' ) |
| 60 | { |
| 61 | curpos [ 0 ] += pointsize / 2.0f ; |
| 62 | return pointsize / 2.0f ; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | We might want to consider making some absent characters from |
| 68 | others (if they exist): lowercase 'l' could be made into digit '1' |
| 69 | or letter 'O' into digit '0'...or vice versa. We could also |
| 70 | make 'b', 'd', 'p' and 'q' by mirror-imaging - this would |
| 71 | save a little more texture memory in some fonts. |
| 72 | */ |
| 73 | |
| 74 | if ( ! exists [ cc ] ) |
| 75 | return 0.0f ; |
| 76 | |
| 77 | glBegin ( GL_TRIANGLE_STRIP ) ; |
| 78 | glTexCoord2f ( t_left [cc], t_bot[cc] ) ; |
| 79 | glVertex3f ( curpos[0] + v_left [cc] * pointsize, |
| 80 | curpos[1] + v_bot [cc] * pointsize, |
| 81 | curpos[2] ) ; |
| 82 | |
| 83 | glTexCoord2f ( t_left [cc], t_top[cc] ) ; |
| 84 | glVertex3f ( curpos[0] + (italic + v_left [cc]) * pointsize, |
| 85 | curpos[1] + v_top [cc] * pointsize, |
| 86 | curpos[2] ) ; |
| 87 | |
| 88 | glTexCoord2f ( t_right[cc], t_bot[cc] ) ; |
| 89 | glVertex3f ( curpos[0] + v_right[cc] * pointsize, |
| 90 | curpos[1] + v_bot [cc] * pointsize, |
| 91 | curpos[2] ) ; |
| 92 | |
| 93 | glTexCoord2f ( t_right[cc], t_top[cc] ) ; |
| 94 | glVertex3f ( curpos[0] + (italic + v_right[cc]) * pointsize, |
| 95 | curpos[1] + v_top [cc] * pointsize, |
| 96 | curpos[2] ) ; |
| 97 | glEnd () ; |
| 98 | |
| 99 | float ww = ( gap + ( fixed_pitch ? width : widths[cc] ) ) * pointsize ; |
| 100 | curpos[0] += ww ; |
| 101 | return ww ; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | void fntTexFont::setGlyph ( char c, float wid, |
| 107 | float tex_left, float tex_right, |
| 108 | float tex_bot , float tex_top , |
| 109 | float vtx_left, float vtx_right, |
| 110 | float vtx_bot , float vtx_top ) |
| 111 | { |
| 112 | unsigned int cc = (unsigned char) c ; |
| 113 | |
| 114 | exists[cc] = FNT_TRUE ; |
| 115 | |
| 116 | widths[cc] = wid; |
| 117 | |
| 118 | t_left[cc] = tex_left ; t_right[cc] = tex_right ; |
| 119 | t_bot [cc] = tex_bot ; t_top [cc] = tex_top ; |
| 120 | |
| 121 | v_left[cc] = vtx_left ; v_right[cc] = vtx_right ; |
| 122 | v_bot [cc] = vtx_bot ; v_top [cc] = vtx_top ; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | int fntTexFont::getGlyph ( char c, float* wid, |
| 127 | float *tex_left, float *tex_right, |
| 128 | float *tex_bot , float *tex_top , |
| 129 | float *vtx_left, float *vtx_right, |
| 130 | float *vtx_bot , float *vtx_top ) |
| 131 | { |
| 132 | unsigned int cc = (unsigned char) c ; |
| 133 | |
| 134 | if ( ! exists[cc] ) return FNT_FALSE ; |
| 135 | |
| 136 | if ( wid != NULL ) *wid = widths [cc] ; |
| 137 | |
| 138 | if ( tex_left != NULL ) *tex_left = t_left [cc] ; |
| 139 | if ( tex_right != NULL ) *tex_right = t_right[cc] ; |
| 140 | if ( tex_bot != NULL ) *tex_bot = t_bot [cc] ; |
| 141 | if ( tex_top != NULL ) *tex_top = t_top [cc] ; |
| 142 | |
| 143 | if ( vtx_left != NULL ) *vtx_left = v_left [cc] ; |
| 144 | if ( vtx_right != NULL ) *vtx_right = v_right[cc] ; |
| 145 | if ( vtx_bot != NULL ) *vtx_bot = v_bot [cc] ; |
| 146 | if ( vtx_top != NULL ) *vtx_top = v_top [cc] ; |
| 147 | |
| 148 | return FNT_TRUE ; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void fntTexFont::getBBox ( const char *s, |
| 153 | float pointsize, float italic, |
| 154 | float *left, float *right, |
| 155 | float *bot , float *top ) |
| 156 | { |
| 157 | float h_pos = 0.0f ; |
| 158 | float v_pos = 0.0f ; |
| 159 | float l, r, b, t ; |
| 160 | |
| 161 | l = r = b = t = 0.0f ; |
| 162 | |
| 163 | while ( *s != '\0' ) |
| 164 | { |
| 165 | if ( *s == '\n' ) |
| 166 | { |
| 167 | h_pos = 0.0f ; |
| 168 | v_pos -= 1.333f ; |
| 169 | s++ ; |
| 170 | continue ; |
| 171 | } |
| 172 | |
| 173 | unsigned int cc = (unsigned char) *(s++) ; |
| 174 | |
| 175 | if ( ! exists [ cc ] ) |
| 176 | { |
| 177 | if ( cc >= 'A' && cc <= 'Z' ) |
| 178 | cc = cc - 'A' + 'a' ; |
| 179 | else |
| 180 | if ( cc >= 'a' && cc <= 'z' ) |
| 181 | cc = cc - 'a' + 'A' ; |
| 182 | |
| 183 | if ( cc == ' ' ) |
| 184 | { |
| 185 | r += 0.5f ; |
| 186 | h_pos += 0.5f ; |
| 187 | |
| 188 | continue ; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if ( ! exists [ cc ] ) |
| 193 | continue ; |
| 194 | |
| 195 | if ( italic >= 0 ) |
| 196 | { |
| 197 | if ( l > h_pos + v_left [cc] ) l = h_pos + v_left [cc] ; |
| 198 | if ( r < gap + h_pos + v_right[cc]+italic ) r = gap + h_pos + v_right[cc] + italic ; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | if ( l > h_pos + v_left [cc]+italic ) l = h_pos + v_left [cc] + italic ; |
| 203 | if ( r < gap + h_pos + v_right[cc] ) r = gap + h_pos + v_right[cc] ; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | if ( b > v_pos + v_bot [cc] ) b = v_pos + v_bot [cc] ; |
| 208 | if ( t < v_pos + v_top [cc] ) t = v_pos + v_top [cc] ; |
| 209 | |
| 210 | h_pos += gap + ( fixed_pitch ? width : widths[cc] ) ; |
| 211 | } |
| 212 | |
| 213 | if ( left != NULL ) *left = l * pointsize ; |
| 214 | if ( right != NULL ) *right = r * pointsize ; |
| 215 | if ( top != NULL ) *top = t * pointsize ; |
| 216 | if ( bot != NULL ) *bot = b * pointsize ; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void fntTexFont::puts ( sgVec3 curpos, float pointsize, float italic, const char *s ) |
| 221 | { |
| 222 | SGfloat origx = curpos[0] ; |
| 223 | |
| 224 | if ( ! bound ) |
| 225 | bind_texture () ; |
| 226 | |
| 227 | while ( *s != '\0' ) |
| 228 | { |
| 229 | if (*s == '\n') |
| 230 | { |
| 231 | curpos[0] = origx ; |
| 232 | curpos[1] -= pointsize ; |
| 233 | } |
| 234 | else |
| 235 | low_putch ( curpos, pointsize, italic, *s ) ; |
| 236 | |
| 237 | s++ ; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void fntInit () |
| 242 | { |
| 243 | /* Empty right now */ |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void fntSetError(int level, const char* format, ...) |
| 248 | { |
| 249 | const int bufferLength = 2048; |
| 250 | char buffer[2048]; |
| 251 | va_list args; |
| 252 | va_start (args, format); |
| 253 | vsnprintf(buffer,bufferLength,format, args); |
| 254 | va_end (args); |
| 255 | |
| 256 | SG_LOG(SG_GUI, static_cast<sgDebugPriority>(level), buffer); |
| 257 | } |