| 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 |
#include "tr_common.h" |
| 72 |
#include "../qcommon/qcommon.h" |
| 73 |
|
| 74 |
#ifdef BUILD_FREETYPE |
| 75 |
#include <ft2build.h> |
| 76 |
#include FT_FREETYPE_H |
| 77 |
#include FT_ERRORS_H |
| 78 |
#include FT_SYSTEM_H |
| 79 |
#include FT_IMAGE_H |
| 80 |
#include FT_OUTLINE_H |
| 81 |
|
| 82 |
#define _FLOOR(x) ((x) & -64) |
| 83 |
#define _CEIL(x) (((x)+63) & -64) |
| 84 |
#define _TRUNC(x) ((x) >> 6) |
| 85 |
|
| 86 |
FT_Library ftLibrary = NULL; |
| 87 |
#endif |
| 88 |
|
| 89 |
#define MAX_FONTS 6 |
| 90 |
static int registeredFontCount = 0; |
| 91 |
static fontInfo_t registeredFont[MAX_FONTS]; |
| 92 |
|
| 93 |
#ifdef BUILD_FREETYPE |
| 94 |
void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *top, int *bottom, int *height, int *pitch) { |
| 95 |
*left = _FLOOR( glyph->metrics.horiBearingX ); |
| 96 |
*right = _CEIL( glyph->metrics.horiBearingX + glyph->metrics.width ); |
| 97 |
*width = _TRUNC(*right - *left); |
| 98 |
|
| 99 |
*top = _CEIL( glyph->metrics.horiBearingY ); |
| 100 |
*bottom = _FLOOR( glyph->metrics.horiBearingY - glyph->metrics.height ); |
| 101 |
*height = _TRUNC( *top - *bottom ); |
| 102 |
*pitch = ( qtrue ? (*width+3) & -4 : (*width+7) >> 3 ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) { |
| 107 |
FT_Bitmap *bit2; |
| 108 |
int left, right, width, top, bottom, height, pitch, size; |
| 109 |
|
| 110 |
R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch); |
| 111 |
|
| 112 |
if ( glyph->format == ft_glyph_format_outline ) { |
| 113 |
size = pitch*height; |
| 114 |
|
| 115 |
bit2 = ri.Malloc(sizeof(FT_Bitmap)); |
| 116 |
|
| 117 |
bit2->width = width; |
| 118 |
bit2->rows = height; |
| 119 |
bit2->pitch = pitch; |
| 120 |
bit2->pixel_mode = ft_pixel_mode_grays; |
| 121 |
|
| 122 |
bit2->buffer = ri.Malloc(pitch*height); |
| 123 |
bit2->num_grays = 256; |
| 124 |
|
| 125 |
Com_Memset( bit2->buffer, 0, size ); |
| 126 |
|
| 127 |
FT_Outline_Translate( &glyph->outline, -left, -bottom ); |
| 128 |
|
| 129 |
FT_Outline_Get_Bitmap( ftLibrary, &glyph->outline, bit2 ); |
| 130 |
|
| 131 |
glyphOut->height = height; |
| 132 |
glyphOut->pitch = pitch; |
| 133 |
glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1; |
| 134 |
glyphOut->bottom = bottom; |
| 135 |
|
| 136 |
return bit2; |
| 137 |
} else { |
| 138 |
ri.Printf(PRINT_ALL, "Non-outline fonts are not supported\n"); |
| 139 |
} |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
void WriteTGA (char *filename, byte *data, int width, int height) { |
| 144 |
byte *buffer; |
| 145 |
int i, c; |
| 146 |
int row; |
| 147 |
unsigned char *flip; |
| 148 |
unsigned char *src, *dst; |
| 149 |
|
| 150 |
buffer = ri.Malloc(width*height*4 + 18); |
| 151 |
Com_Memset (buffer, 0, 18); |
| 152 |
buffer[2] = 2; |
| 153 |
buffer[12] = width&255; |
| 154 |
buffer[13] = width>>8; |
| 155 |
buffer[14] = height&255; |
| 156 |
buffer[15] = height>>8; |
| 157 |
buffer[16] = 32; |
| 158 |
|
| 159 |
|
| 160 |
c = 18 + width * height * 4; |
| 161 |
for (i=18 ; i<c ; i+=4) |
| 162 |
{ |
| 163 |
buffer[i] = data[i-18+2]; |
| 164 |
buffer[i+1] = data[i-18+1]; |
| 165 |
buffer[i+2] = data[i-18+0]; |
| 166 |
buffer[i+3] = data[i-18+3]; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
flip = (unsigned char *)ri.Malloc(width*4); |
| 171 |
for(row = 0; row < height/2; row++) |
| 172 |
{ |
| 173 |
src = buffer + 18 + row * 4 * width; |
| 174 |
dst = buffer + 18 + (height - row - 1) * 4 * width; |
| 175 |
|
| 176 |
Com_Memcpy(flip, src, width*4); |
| 177 |
Com_Memcpy(src, dst, width*4); |
| 178 |
Com_Memcpy(dst, flip, width*4); |
| 179 |
} |
| 180 |
ri.Free(flip); |
| 181 |
|
| 182 |
ri.FS_WriteFile(filename, buffer, c); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
ri.Free (buffer); |
| 189 |
} |
| 190 |
|
| 191 |
static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, int *yOut, int *maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight) { |
| 192 |
int i; |
| 193 |
static glyphInfo_t glyph; |
| 194 |
unsigned char *src, *dst; |
| 195 |
float scaled_width, scaled_height; |
| 196 |
FT_Bitmap *bitmap = NULL; |
| 197 |
|
| 198 |
Com_Memset(&glyph, 0, sizeof(glyphInfo_t)); |
| 199 |
|
| 200 |
if (face != NULL) { |
| 201 |
FT_Load_Glyph(face, FT_Get_Char_Index( face, c), FT_LOAD_DEFAULT ); |
| 202 |
bitmap = R_RenderGlyph(face->glyph, &glyph); |
| 203 |
if (bitmap) { |
| 204 |
glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1; |
| 205 |
} else { |
| 206 |
return &glyph; |
| 207 |
} |
| 208 |
|
| 209 |
if (glyph.height > *maxHeight) { |
| 210 |
*maxHeight = glyph.height; |
| 211 |
} |
| 212 |
|
| 213 |
if (calcHeight) { |
| 214 |
ri.Free(bitmap->buffer); |
| 215 |
ri.Free(bitmap); |
| 216 |
return &glyph; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
scaled_width = glyph.pitch; |
| 229 |
scaled_height = glyph.height; |
| 230 |
|
| 231 |
|
| 232 |
if (*xOut + scaled_width + 1 >= 255) { |
| 233 |
*xOut = 0; |
| 234 |
*yOut += *maxHeight + 1; |
| 235 |
} |
| 236 |
|
| 237 |
if (*yOut + *maxHeight + 1 >= 255) { |
| 238 |
*yOut = -1; |
| 239 |
*xOut = -1; |
| 240 |
ri.Free(bitmap->buffer); |
| 241 |
ri.Free(bitmap); |
| 242 |
return &glyph; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
src = bitmap->buffer; |
| 247 |
dst = imageOut + (*yOut * 256) + *xOut; |
| 248 |
|
| 249 |
if (bitmap->pixel_mode == ft_pixel_mode_mono) { |
| 250 |
for (i = 0; i < glyph.height; i++) { |
| 251 |
int j; |
| 252 |
unsigned char *_src = src; |
| 253 |
unsigned char *_dst = dst; |
| 254 |
unsigned char mask = 0x80; |
| 255 |
unsigned char val = *_src; |
| 256 |
for (j = 0; j < glyph.pitch; j++) { |
| 257 |
if (mask == 0x80) { |
| 258 |
val = *_src++; |
| 259 |
} |
| 260 |
if (val & mask) { |
| 261 |
*_dst = 0xff; |
| 262 |
} |
| 263 |
mask >>= 1; |
| 264 |
|
| 265 |
if ( mask == 0 ) { |
| 266 |
mask = 0x80; |
| 267 |
} |
| 268 |
_dst++; |
| 269 |
} |
| 270 |
|
| 271 |
src += glyph.pitch; |
| 272 |
dst += 256; |
| 273 |
} |
| 274 |
} else { |
| 275 |
for (i = 0; i < glyph.height; i++) { |
| 276 |
Com_Memcpy(dst, src, glyph.pitch); |
| 277 |
src += glyph.pitch; |
| 278 |
dst += 256; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
glyph.imageHeight = scaled_height; |
| 286 |
glyph.imageWidth = scaled_width; |
| 287 |
glyph.s = (float)*xOut / 256; |
| 288 |
glyph.t = (float)*yOut / 256; |
| 289 |
glyph.s2 = glyph.s + (float)scaled_width / 256; |
| 290 |
glyph.t2 = glyph.t + (float)scaled_height / 256; |
| 291 |
|
| 292 |
*xOut += scaled_width + 1; |
| 293 |
|
| 294 |
ri.Free(bitmap->buffer); |
| 295 |
ri.Free(bitmap); |
| 296 |
} |
| 297 |
|
| 298 |
return &glyph; |
| 299 |
} |
| 300 |
#endif |
| 301 |
|
| 302 |
static int fdOffset; |
| 303 |
static byte *fdFile; |
| 304 |
|
| 305 |
int readInt( void ) { |
| 306 |
int i = ((unsigned int)fdFile[fdOffset] | ((unsigned int)fdFile[fdOffset+1]<<8) | ((unsigned int)fdFile[fdOffset+2]<<16) | ((unsigned int)fdFile[fdOffset+3]<<24)); |
| 307 |
fdOffset += 4; |
| 308 |
return i; |
| 309 |
} |
| 310 |
|
| 311 |
typedef union { |
| 312 |
byte fred[4]; |
| 313 |
float ffred; |
| 314 |
} poor; |
| 315 |
|
| 316 |
float readFloat( void ) { |
| 317 |
poor me; |
| 318 |
#if defined Q3_BIG_ENDIAN |
| 319 |
me.fred[0] = fdFile[fdOffset+3]; |
| 320 |
me.fred[1] = fdFile[fdOffset+2]; |
| 321 |
me.fred[2] = fdFile[fdOffset+1]; |
| 322 |
me.fred[3] = fdFile[fdOffset+0]; |
| 323 |
#elif defined Q3_LITTLE_ENDIAN |
| 324 |
me.fred[0] = fdFile[fdOffset+0]; |
| 325 |
me.fred[1] = fdFile[fdOffset+1]; |
| 326 |
me.fred[2] = fdFile[fdOffset+2]; |
| 327 |
me.fred[3] = fdFile[fdOffset+3]; |
| 328 |
#endif |
| 329 |
fdOffset += 4; |
| 330 |
return me.ffred; |
| 331 |
} |
| 332 |
|
| 333 |
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) { |
| 334 |
#ifdef BUILD_FREETYPE |
| 335 |
FT_Face face; |
| 336 |
int j, k, xOut, yOut, lastStart, imageNumber; |
| 337 |
int scaledSize, newSize, maxHeight, left; |
| 338 |
unsigned char *out, *imageBuff; |
| 339 |
glyphInfo_t *glyph; |
| 340 |
image_t *image; |
| 341 |
qhandle_t h; |
| 342 |
float max; |
| 343 |
float dpi = 72; |
| 344 |
float glyphScale; |
| 345 |
#endif |
| 346 |
void *faceData; |
| 347 |
int i, len; |
| 348 |
char name[1024]; |
| 349 |
|
| 350 |
if (!fontName) { |
| 351 |
ri.Printf(PRINT_ALL, "RE_RegisterFont: called with empty name\n"); |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
if (pointSize <= 0) { |
| 356 |
pointSize = 12; |
| 357 |
} |
| 358 |
|
| 359 |
R_IssuePendingRenderCommands(); |
| 360 |
|
| 361 |
if (registeredFontCount >= MAX_FONTS) { |
| 362 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: Too many fonts registered already.\n"); |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
Com_sprintf(name, sizeof(name), "fonts/fontImage_%i.dat",pointSize); |
| 367 |
for (i = 0; i < registeredFontCount; i++) { |
| 368 |
if (Q_stricmp(name, registeredFont[i].name) == 0) { |
| 369 |
Com_Memcpy(font, ®isteredFont[i], sizeof(fontInfo_t)); |
| 370 |
return; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
len = ri.FS_ReadFile(name, NULL); |
| 375 |
if (len == sizeof(fontInfo_t)) { |
| 376 |
ri.FS_ReadFile(name, &faceData); |
| 377 |
fdOffset = 0; |
| 378 |
fdFile = faceData; |
| 379 |
for(i=0; i<GLYPHS_PER_FONT; i++) { |
| 380 |
font->glyphs[i].height = readInt(); |
| 381 |
font->glyphs[i].top = readInt(); |
| 382 |
font->glyphs[i].bottom = readInt(); |
| 383 |
font->glyphs[i].pitch = readInt(); |
| 384 |
font->glyphs[i].xSkip = readInt(); |
| 385 |
font->glyphs[i].imageWidth = readInt(); |
| 386 |
font->glyphs[i].imageHeight = readInt(); |
| 387 |
font->glyphs[i].s = readFloat(); |
| 388 |
font->glyphs[i].t = readFloat(); |
| 389 |
font->glyphs[i].s2 = readFloat(); |
| 390 |
font->glyphs[i].t2 = readFloat(); |
| 391 |
font->glyphs[i].glyph = readInt(); |
| 392 |
Q_strncpyz(font->glyphs[i].shaderName, (const char *)&fdFile[fdOffset], sizeof(font->glyphs[i].shaderName)); |
| 393 |
fdOffset += sizeof(font->glyphs[i].shaderName); |
| 394 |
} |
| 395 |
font->glyphScale = readFloat(); |
| 396 |
Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH); |
| 397 |
|
| 398 |
|
| 399 |
Q_strncpyz(font->name, name, sizeof(font->name)); |
| 400 |
for (i = GLYPH_START; i <= GLYPH_END; i++) { |
| 401 |
font->glyphs[i].glyph = RE_RegisterShaderNoMip(font->glyphs[i].shaderName); |
| 402 |
} |
| 403 |
Com_Memcpy(®isteredFont[registeredFontCount++], font, sizeof(fontInfo_t)); |
| 404 |
ri.FS_FreeFile(faceData); |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
#ifndef BUILD_FREETYPE |
| 409 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType code not available\n"); |
| 410 |
#else |
| 411 |
if (ftLibrary == NULL) { |
| 412 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType not initialized.\n"); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
len = ri.FS_ReadFile(fontName, &faceData); |
| 417 |
if (len <= 0) { |
| 418 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: Unable to read font file '%s'\n", fontName); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if (FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face )) { |
| 424 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, unable to allocate new face.\n"); |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
if (FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi)) { |
| 430 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, unable to set face char size.\n"); |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
out = ri.Malloc(256*256); |
| 440 |
if (out == NULL) { |
| 441 |
ri.Printf(PRINT_WARNING, "RE_RegisterFont: ri.Malloc failure during output image creation.\n"); |
| 442 |
return; |
| 443 |
} |
| 444 |
Com_Memset(out, 0, 256*256); |
| 445 |
|
| 446 |
maxHeight = 0; |
| 447 |
|
| 448 |
for (i = GLYPH_START; i <= GLYPH_END; i++) { |
| 449 |
RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue); |
| 450 |
} |
| 451 |
|
| 452 |
xOut = 0; |
| 453 |
yOut = 0; |
| 454 |
i = GLYPH_START; |
| 455 |
lastStart = i; |
| 456 |
imageNumber = 0; |
| 457 |
|
| 458 |
while ( i <= GLYPH_END + 1 ) { |
| 459 |
|
| 460 |
if ( i == GLYPH_END + 1 ) { |
| 461 |
|
| 462 |
xOut = yOut = -1; |
| 463 |
} else { |
| 464 |
glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse); |
| 465 |
} |
| 466 |
|
| 467 |
if (xOut == -1 || yOut == -1) { |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
scaledSize = 256*256; |
| 473 |
newSize = scaledSize * 4; |
| 474 |
imageBuff = ri.Malloc(newSize); |
| 475 |
left = 0; |
| 476 |
max = 0; |
| 477 |
for ( k = 0; k < (scaledSize) ; k++ ) { |
| 478 |
if (max < out[k]) { |
| 479 |
max = out[k]; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if (max > 0) { |
| 484 |
max = 255/max; |
| 485 |
} |
| 486 |
|
| 487 |
for ( k = 0; k < (scaledSize) ; k++ ) { |
| 488 |
imageBuff[left++] = 255; |
| 489 |
imageBuff[left++] = 255; |
| 490 |
imageBuff[left++] = 255; |
| 491 |
|
| 492 |
imageBuff[left++] = ((float)out[k] * max); |
| 493 |
} |
| 494 |
|
| 495 |
Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize); |
| 496 |
if (r_saveFontData->integer) { |
| 497 |
WriteTGA(name, imageBuff, 256, 256); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
image = R_CreateImage(name, imageBuff, 256, 256, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 ); |
| 502 |
h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse); |
| 503 |
for (j = lastStart; j < i; j++) { |
| 504 |
font->glyphs[j].glyph = h; |
| 505 |
Q_strncpyz(font->glyphs[j].shaderName, name, sizeof(font->glyphs[j].shaderName)); |
| 506 |
} |
| 507 |
lastStart = i; |
| 508 |
Com_Memset(out, 0, 256*256); |
| 509 |
xOut = 0; |
| 510 |
yOut = 0; |
| 511 |
ri.Free(imageBuff); |
| 512 |
if ( i == GLYPH_END + 1 ) |
| 513 |
i++; |
| 514 |
} else { |
| 515 |
Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t)); |
| 516 |
i++; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
glyphScale = 72.0f / dpi; |
| 522 |
|
| 523 |
|
| 524 |
glyphScale *= 48.0f / pointSize; |
| 525 |
|
| 526 |
registeredFont[registeredFontCount].glyphScale = glyphScale; |
| 527 |
font->glyphScale = glyphScale; |
| 528 |
Com_Memcpy(®isteredFont[registeredFontCount++], font, sizeof(fontInfo_t)); |
| 529 |
|
| 530 |
if (r_saveFontData->integer) { |
| 531 |
ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t)); |
| 532 |
} |
| 533 |
|
| 534 |
ri.Free(out); |
| 535 |
|
| 536 |
ri.FS_FreeFile(faceData); |
| 537 |
#endif |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
void R_InitFreeType(void) { |
| 543 |
#ifdef BUILD_FREETYPE |
| 544 |
if (FT_Init_FreeType( &ftLibrary )) { |
| 545 |
ri.Printf(PRINT_WARNING, "R_InitFreeType: Unable to initialize FreeType.\n"); |
| 546 |
} |
| 547 |
#endif |
| 548 |
registeredFontCount = 0; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
void R_DoneFreeType(void) { |
| 553 |
#ifdef BUILD_FREETYPE |
| 554 |
if (ftLibrary) { |
| 555 |
FT_Done_FreeType( ftLibrary ); |
| 556 |
ftLibrary = NULL; |
| 557 |
} |
| 558 |
#endif |
| 559 |
registeredFontCount = 0; |
| 560 |
} |
| 561 |
|