tr_font.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. #ifdef BUILD_FREETYPE
  24. #include "../ft2/fterrors.h"
  25. #include "../ft2/ftsystem.h"
  26. #include "../ft2/ftimage.h"
  27. #include "../ft2/freetype.h"
  28. #include "../ft2/ftoutln.h"
  29. #define _FLOOR(x) ((x) & -64)
  30. #define _CEIL(x) (((x)+63) & -64)
  31. #define _TRUNC(x) ((x) >> 6)
  32. FT_Library ftLibrary = NULL;
  33. #endif
  34. #ifdef BUILD_FREETYPE
  35. /*
  36. ============
  37. R_GetGlyphInfo
  38. ============
  39. */
  40. void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *top, int *bottom, int *height, int *pitch) {
  41. *left = _FLOOR( glyph->metrics.horiBearingX );
  42. *right = _CEIL( glyph->metrics.horiBearingX + glyph->metrics.width );
  43. *width = _TRUNC(*right - *left);
  44. *top = _CEIL( glyph->metrics.horiBearingY );
  45. *bottom = _FLOOR( glyph->metrics.horiBearingY - glyph->metrics.height );
  46. *height = _TRUNC( *top - *bottom );
  47. *pitch = ( qtrue ? (*width+3) & -4 : (*width+7) >> 3 );
  48. }
  49. /*
  50. ============
  51. R_RenderGlyph
  52. ============
  53. */
  54. FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
  55. FT_Bitmap *bit2;
  56. int left, right, width, top, bottom, height, pitch, size;
  57. R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch);
  58. if ( glyph->format == ft_glyph_format_outline ) {
  59. size = pitch*height;
  60. bit2 = Mem_Alloc(sizeof(FT_Bitmap));
  61. bit2->width = width;
  62. bit2->rows = height;
  63. bit2->pitch = pitch;
  64. bit2->pixel_mode = ft_pixel_mode_grays;
  65. //bit2->pixel_mode = ft_pixel_mode_mono;
  66. bit2->buffer = Mem_Alloc(pitch*height);
  67. bit2->num_grays = 256;
  68. memset( bit2->buffer, 0, size );
  69. FT_Outline_Translate( &glyph->outline, -left, -bottom );
  70. FT_Outline_Get_Bitmap( ftLibrary, &glyph->outline, bit2 );
  71. glyphOut->height = height;
  72. glyphOut->pitch = pitch;
  73. glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1;
  74. glyphOut->bottom = bottom;
  75. return bit2;
  76. }
  77. else {
  78. common->Printf( "Non-outline fonts are not supported\n" );
  79. }
  80. return NULL;
  81. }
  82. /*
  83. ============
  84. RE_ConstructGlyphInfo
  85. ============
  86. */
  87. glyphInfo_t *RE_ConstructGlyphInfo( unsigned char *imageOut, int *xOut, int *yOut, int *maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight ) {
  88. int i;
  89. static glyphInfo_t glyph;
  90. unsigned char *src, *dst;
  91. float scaled_width, scaled_height;
  92. FT_Bitmap *bitmap = NULL;
  93. memset(&glyph, 0, sizeof(glyphInfo_t));
  94. // make sure everything is here
  95. if (face != NULL) {
  96. FT_Load_Glyph(face, FT_Get_Char_Index( face, c), FT_LOAD_DEFAULT );
  97. bitmap = R_RenderGlyph(face->glyph, &glyph);
  98. if (bitmap) {
  99. glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1;
  100. } else {
  101. return &glyph;
  102. }
  103. if (glyph.height > *maxHeight) {
  104. *maxHeight = glyph.height;
  105. }
  106. if (calcHeight) {
  107. Mem_Free(bitmap->buffer);
  108. Mem_Free(bitmap);
  109. return &glyph;
  110. }
  111. /*
  112. // need to convert to power of 2 sizes so we do not get
  113. // any scaling from the gl upload
  114. for (scaled_width = 1 ; scaled_width < glyph.pitch ; scaled_width<<=1)
  115. ;
  116. for (scaled_height = 1 ; scaled_height < glyph.height ; scaled_height<<=1)
  117. ;
  118. */
  119. scaled_width = glyph.pitch;
  120. scaled_height = glyph.height;
  121. // we need to make sure we fit
  122. if (*xOut + scaled_width + 1 >= 255) {
  123. if (*yOut + *maxHeight + 1 >= 255) {
  124. *yOut = -1;
  125. *xOut = -1;
  126. Mem_Free(bitmap->buffer);
  127. Mem_Free(bitmap);
  128. return &glyph;
  129. } else {
  130. *xOut = 0;
  131. *yOut += *maxHeight + 1;
  132. }
  133. } else if (*yOut + *maxHeight + 1 >= 255) {
  134. *yOut = -1;
  135. *xOut = -1;
  136. Mem_Free(bitmap->buffer);
  137. Mem_Free(bitmap);
  138. return &glyph;
  139. }
  140. src = bitmap->buffer;
  141. dst = imageOut + (*yOut * 256) + *xOut;
  142. if (bitmap->pixel_mode == ft_pixel_mode_mono) {
  143. for (i = 0; i < glyph.height; i++) {
  144. int j;
  145. unsigned char *_src = src;
  146. unsigned char *_dst = dst;
  147. unsigned char mask = 0x80;
  148. unsigned char val = *_src;
  149. for (j = 0; j < glyph.pitch; j++) {
  150. if (mask == 0x80) {
  151. val = *_src++;
  152. }
  153. if (val & mask) {
  154. *_dst = 0xff;
  155. }
  156. mask >>= 1;
  157. if ( mask == 0 ) {
  158. mask = 0x80;
  159. }
  160. _dst++;
  161. }
  162. src += glyph.pitch;
  163. dst += 256;
  164. }
  165. } else {
  166. for (i = 0; i < glyph.height; i++) {
  167. memcpy( dst, src, glyph.pitch );
  168. src += glyph.pitch;
  169. dst += 256;
  170. }
  171. }
  172. // we now have an 8 bit per pixel grey scale bitmap
  173. // that is width wide and pf->ftSize->metrics.y_ppem tall
  174. glyph.imageHeight = scaled_height;
  175. glyph.imageWidth = scaled_width;
  176. glyph.s = (float)*xOut / 256;
  177. glyph.t = (float)*yOut / 256;
  178. glyph.s2 = glyph.s + (float)scaled_width / 256;
  179. glyph.t2 = glyph.t + (float)scaled_height / 256;
  180. *xOut += scaled_width + 1;
  181. }
  182. Mem_Free(bitmap->buffer);
  183. Mem_Free(bitmap);
  184. return &glyph;
  185. }
  186. #endif
  187. static int fdOffset;
  188. static byte *fdFile;
  189. /*
  190. ============
  191. readInt
  192. ============
  193. */
  194. int readInt( void ) {
  195. int i = fdFile[fdOffset]+(fdFile[fdOffset+1]<<8)+(fdFile[fdOffset+2]<<16)+(fdFile[fdOffset+3]<<24);
  196. fdOffset += 4;
  197. return i;
  198. }
  199. typedef union {
  200. byte fred[4];
  201. float ffred;
  202. } poor;
  203. /*
  204. ============
  205. readFloat
  206. ============
  207. */
  208. float readFloat( void ) {
  209. poor me;
  210. #ifdef __ppc__
  211. me.fred[0] = fdFile[fdOffset+3];
  212. me.fred[1] = fdFile[fdOffset+2];
  213. me.fred[2] = fdFile[fdOffset+1];
  214. me.fred[3] = fdFile[fdOffset+0];
  215. #else
  216. me.fred[0] = fdFile[fdOffset+0];
  217. me.fred[1] = fdFile[fdOffset+1];
  218. me.fred[2] = fdFile[fdOffset+2];
  219. me.fred[3] = fdFile[fdOffset+3];
  220. #endif
  221. fdOffset += 4;
  222. return me.ffred;
  223. }
  224. /*
  225. ============
  226. RegisterFont
  227. Loads 3 point sizes, 12, 24, and 48
  228. ============
  229. */
  230. bool idRenderSystemLocal::RegisterFont( const char *fontName, fontInfoEx_t &font ) {
  231. #ifdef BUILD_FREETYPE
  232. FT_Face face;
  233. int j, k, xOut, yOut, lastStart, imageNumber;
  234. int scaledSize, newSize, maxHeight, left, satLevels;
  235. unsigned char *out, *imageBuff;
  236. glyphInfo_t *glyph;
  237. idImage *image;
  238. idMaterial *h;
  239. float max;
  240. #endif
  241. void *faceData;
  242. ID_TIME_T ftime;
  243. int i, len, fontCount;
  244. char name[1024];
  245. int pointSize = 12;
  246. /*
  247. if ( registeredFontCount >= MAX_FONTS ) {
  248. common->Warning( "RegisterFont: Too many fonts registered already." );
  249. return false;
  250. }
  251. int pointSize = 12;
  252. idStr::snPrintf( name, sizeof(name), "%s/fontImage_%i.dat", fontName, pointSize );
  253. for ( i = 0; i < registeredFontCount; i++ ) {
  254. if ( idStr::Icmp(name, registeredFont[i].fontInfoSmall.name) == 0 ) {
  255. memcpy( &font, &registeredFont[i], sizeof( fontInfoEx_t ) );
  256. return true;
  257. }
  258. }
  259. */
  260. memset( &font, 0, sizeof( font ) );
  261. for ( fontCount = 0; fontCount < 3; fontCount++ ) {
  262. if ( fontCount == 0) {
  263. pointSize = 12;
  264. } else if ( fontCount == 1 ) {
  265. pointSize = 24;
  266. } else {
  267. pointSize = 48;
  268. }
  269. // we also need to adjust the scale based on point size relative to 48 points as the ui scaling is based on a 48 point font
  270. float glyphScale = 1.0f; // change the scale to be relative to 1 based on 72 dpi ( so dpi of 144 means a scale of .5 )
  271. glyphScale *= 48.0f / pointSize;
  272. idStr::snPrintf( name, sizeof(name), "%s/fontImage_%i.dat", fontName, pointSize );
  273. fontInfo_t *outFont;
  274. if ( fontCount == 0 ) {
  275. outFont = &font.fontInfoSmall;
  276. }
  277. else if ( fontCount == 1 ) {
  278. outFont = &font.fontInfoMedium;
  279. }
  280. else {
  281. outFont = &font.fontInfoLarge;
  282. }
  283. idStr::Copynz( outFont->name, name, sizeof( outFont->name ) );
  284. len = fileSystem->ReadFile( name, NULL, &ftime );
  285. if ( len != sizeof( fontInfo_t ) ) {
  286. common->Warning( "RegisterFont: couldn't find font: '%s'", name );
  287. return false;
  288. }
  289. fileSystem->ReadFile( name, &faceData, &ftime );
  290. fdOffset = 0;
  291. fdFile = reinterpret_cast<unsigned char*>(faceData);
  292. for( i = 0; i < GLYPHS_PER_FONT; i++ ) {
  293. outFont->glyphs[i].height = readInt();
  294. outFont->glyphs[i].top = readInt();
  295. outFont->glyphs[i].bottom = readInt();
  296. outFont->glyphs[i].pitch = readInt();
  297. outFont->glyphs[i].xSkip = readInt();
  298. outFont->glyphs[i].imageWidth = readInt();
  299. outFont->glyphs[i].imageHeight = readInt();
  300. outFont->glyphs[i].s = readFloat();
  301. outFont->glyphs[i].t = readFloat();
  302. outFont->glyphs[i].s2 = readFloat();
  303. outFont->glyphs[i].t2 = readFloat();
  304. int junk /* font.glyphs[i].glyph */ = readInt();
  305. //FIXME: the +6, -6 skips the embedded fonts/
  306. memcpy( outFont->glyphs[i].shaderName, &fdFile[fdOffset + 6], 32 - 6 );
  307. fdOffset += 32;
  308. }
  309. outFont->glyphScale = readFloat();
  310. int mw = 0;
  311. int mh = 0;
  312. for (i = GLYPH_START; i < GLYPH_END; i++) {
  313. idStr::snPrintf(name, sizeof(name), "%s/%s", fontName, outFont->glyphs[i].shaderName);
  314. outFont->glyphs[i].glyph = declManager->FindMaterial(name);
  315. outFont->glyphs[i].glyph->SetSort( SS_GUI );
  316. if (mh < outFont->glyphs[i].height) {
  317. mh = outFont->glyphs[i].height;
  318. }
  319. if (mw < outFont->glyphs[i].xSkip) {
  320. mw = outFont->glyphs[i].xSkip;
  321. }
  322. }
  323. if (fontCount == 0) {
  324. font.maxWidthSmall = mw;
  325. font.maxHeightSmall = mh;
  326. } else if (fontCount == 1) {
  327. font.maxWidthMedium = mw;
  328. font.maxHeightMedium = mh;
  329. } else {
  330. font.maxWidthLarge = mw;
  331. font.maxHeightLarge = mh;
  332. }
  333. fileSystem->FreeFile( faceData );
  334. }
  335. //memcpy( &registeredFont[registeredFontCount++], &font, sizeof( fontInfoEx_t ) );
  336. return true ;
  337. #ifndef BUILD_FREETYPE
  338. common->Warning( "RegisterFont: couldn't load FreeType code %s", name );
  339. #else
  340. if (ftLibrary == NULL) {
  341. common->Warning( "RegisterFont: FreeType not initialized." );
  342. return;
  343. }
  344. len = fileSystem->ReadFile(fontName, &faceData, &ftime);
  345. if ( len <= 0 ) {
  346. common->Warning( "RegisterFont: Unable to read font file" );
  347. return;
  348. }
  349. // allocate on the stack first in case we fail
  350. if ( FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face ) ) {
  351. common->Warning( "RegisterFont: FreeType2, unable to allocate new face." );
  352. return;
  353. }
  354. if ( FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi) ) {
  355. common->Warning( "RegisterFont: FreeType2, Unable to set face char size." );
  356. return;
  357. }
  358. // font = registeredFonts[registeredFontCount++];
  359. // make a 256x256 image buffer, once it is full, register it, clean it and keep going
  360. // until all glyphs are rendered
  361. out = Mem_Alloc( 1024*1024 );
  362. if ( out == NULL ) {
  363. common->Warning( "RegisterFont: Mem_Alloc failure during output image creation." );
  364. return;
  365. }
  366. memset( out, 0, 1024*1024 );
  367. maxHeight = 0;
  368. for (i = GLYPH_START; i < GLYPH_END; i++) {
  369. glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue);
  370. }
  371. xOut = 0;
  372. yOut = 0;
  373. i = GLYPH_START;
  374. lastStart = i;
  375. imageNumber = 0;
  376. while ( i <= GLYPH_END ) {
  377. glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse);
  378. if (xOut == -1 || yOut == -1 || i == GLYPH_END) {
  379. // ran out of room
  380. // we need to create an image from the bitmap, set all the handles in the glyphs to this point
  381. //
  382. scaledSize = 256*256;
  383. newSize = scaledSize * 4;
  384. imageBuff = Mem_Alloc(newSize);
  385. left = 0;
  386. max = 0;
  387. satLevels = 255;
  388. for ( k = 0; k < (scaledSize) ; k++ ) {
  389. if (max < out[k]) {
  390. max = out[k];
  391. }
  392. }
  393. if (max > 0) {
  394. max = 255/max;
  395. }
  396. for ( k = 0; k < (scaledSize) ; k++ ) {
  397. imageBuff[left++] = 255;
  398. imageBuff[left++] = 255;
  399. imageBuff[left++] = 255;
  400. imageBuff[left++] = ((float)out[k] * max);
  401. }
  402. idStr::snprintf( name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize );
  403. if (r_saveFontData->integer) {
  404. R_WriteTGA(name, imageBuff, 256, 256);
  405. }
  406. //idStr::snprintf( name, sizeof(name), "fonts/fontImage_%i_%i", imageNumber++, pointSize );
  407. image = R_CreateImage(name, imageBuff, 256, 256, qfalse, qfalse, GL_CLAMP);
  408. h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse);
  409. for (j = lastStart; j < i; j++) {
  410. font.glyphs[j].glyph = h;
  411. idStr::Copynz( font.glyphs[j].shaderName, name, sizeof( font.glyphs[j].shaderName ) );
  412. }
  413. lastStart = i;
  414. memset( out, 0, 1024*1024 );
  415. xOut = 0;
  416. yOut = 0;
  417. Mem_Free( imageBuff );
  418. i++;
  419. } else {
  420. memcpy( &font.glyphs[i], glyph, sizeof( glyphInfo_t ) );
  421. i++;
  422. }
  423. }
  424. registeredFont[registeredFontCount].glyphScale = glyphScale;
  425. font.glyphScale = glyphScale;
  426. memcpy( &registeredFont[registeredFontCount++], &font, sizeof( fontInfo_t ) );
  427. if ( r_saveFontData->integer ) {
  428. fileSystem->WriteFile( va( "fonts/fontImage_%i.dat", pointSize), &font, sizeof( fontInfo_t ) );
  429. }
  430. Mem_Free( out );
  431. fileSystem->FreeFile( faceData );
  432. #endif
  433. return true;
  434. }
  435. /*
  436. ============
  437. R_InitFreeType
  438. ============
  439. */
  440. void R_InitFreeType( void ) {
  441. #ifdef BUILD_FREETYPE
  442. if ( FT_Init_FreeType( &ftLibrary ) ) {
  443. common->Printf( "R_InitFreeType: Unable to initialize FreeType.\n" );
  444. }
  445. #endif
  446. // registeredFontCount = 0;
  447. }
  448. /*
  449. ============
  450. R_DoneFreeType
  451. ============
  452. */
  453. void R_DoneFreeType( void ) {
  454. #ifdef BUILD_FREETYPE
  455. if ( ftLibrary ) {
  456. FT_Done_FreeType( ftLibrary );
  457. ftLibrary = NULL;
  458. }
  459. #endif
  460. // registeredFontCount = 0;
  461. }