tr_font.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // tr_font.c
  19. //
  20. //
  21. // The font system uses FreeType 2.x to render TrueType fonts for use within the game.
  22. // As of this writing ( Nov, 2000 ) Team Arena uses these fonts for all of the ui and
  23. // about 90% of the cgame presentation. A few areas of the CGAME were left uses the old
  24. // fonts since the code is shared with standard Q3A.
  25. //
  26. // If you include this font rendering code in a commercial product you MUST include the
  27. // following somewhere with your product, see www.freetype.org for specifics or changes.
  28. // The Freetype code also uses some hinting techniques that MIGHT infringe on patents
  29. // held by apple so be aware of that also.
  30. //
  31. // As of Q3A 1.25+ and Team Arena, we are shipping the game with the font rendering code
  32. // disabled. This removes any potential patent issues and it keeps us from having to
  33. // distribute an actual TrueTrype font which is 1. expensive to do and 2. seems to require
  34. // an act of god to accomplish.
  35. //
  36. // What we did was pre-render the fonts using FreeType ( which is why we leave the FreeType
  37. // credit in the credits ) and then saved off the glyph data and then hand touched up the
  38. // font bitmaps so they scale a bit better in GL.
  39. //
  40. // There are limitations in the way fonts are saved and reloaded in that it is based on
  41. // point size and not name. So if you pre-render Helvetica in 18 point and Impact in 18 point
  42. // you will end up with a single 18 point data file and image set. Typically you will want to
  43. // choose 3 sizes to best approximate the scaling you will be doing in the ui scripting system
  44. //
  45. // In the UI Scripting code, a scale of 1.0 is equal to a 48 point font. In Team Arena, we
  46. // use three or four scales, most of them exactly equaling the specific rendered size. We
  47. // rendered three sizes in Team Arena, 12, 16, and 20.
  48. //
  49. // To generate new font data you need to go through the following steps.
  50. // 1. delete the fontImage_x_xx.tga files and fontImage_xx.dat files from the fonts path.
  51. // 2. in a ui script, specificy a font, smallFont, and bigFont keyword with font name and
  52. // point size. the original TrueType fonts must exist in fonts at this point.
  53. // 3. run the game, you should see things normally.
  54. // 4. Exit the game and there will be three dat files and at least three tga files. The
  55. // tga's are in 256x256 pages so if it takes three images to render a 24 point font you
  56. // will end up with fontImage_0_24.tga through fontImage_2_24.tga
  57. // 5. You will need to flip the tga's in Photoshop as the tga output code writes them upside
  58. // down.
  59. // 6. In future runs of the game, the system looks for these images and data files when a s
  60. // specific point sized font is rendered and loads them for use.
  61. // 7. Because of the original beta nature of the FreeType code you will probably want to hand
  62. // touch the font bitmaps.
  63. //
  64. // Currently a define in the project turns on or off the FreeType code which is currently
  65. // defined out. To pre-render new fonts you need enable the define ( BUILD_FREETYPE ) and
  66. // uncheck the exclude from build check box in the FreeType2 area of the Renderer project.
  67. #include "tr_local.h"
  68. #include "../qcommon/qcommon.h"
  69. #ifdef BUILD_FREETYPE
  70. #include "../ft2/fterrors.h"
  71. #include "../ft2/ftsystem.h"
  72. #include "../ft2/ftimage.h"
  73. #include "../ft2/freetype.h"
  74. #include "../ft2/ftoutln.h"
  75. #define _FLOOR(x) ((x) & -64)
  76. #define _CEIL(x) (((x)+63) & -64)
  77. #define _TRUNC(x) ((x) >> 6)
  78. FT_Library ftLibrary = NULL;
  79. #endif
  80. #define MAX_FONTS 6
  81. static int registeredFontCount = 0;
  82. static fontInfo_t registeredFont[MAX_FONTS];
  83. #ifdef BUILD_FREETYPE
  84. void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *top, int *bottom, int *height, int *pitch) {
  85. *left = _FLOOR( glyph->metrics.horiBearingX );
  86. *right = _CEIL( glyph->metrics.horiBearingX + glyph->metrics.width );
  87. *width = _TRUNC(*right - *left);
  88. *top = _CEIL( glyph->metrics.horiBearingY );
  89. *bottom = _FLOOR( glyph->metrics.horiBearingY - glyph->metrics.height );
  90. *height = _TRUNC( *top - *bottom );
  91. *pitch = ( qtrue ? (*width+3) & -4 : (*width+7) >> 3 );
  92. }
  93. FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
  94. FT_Bitmap *bit2;
  95. int left, right, width, top, bottom, height, pitch, size;
  96. R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch);
  97. if ( glyph->format == ft_glyph_format_outline ) {
  98. size = pitch*height;
  99. bit2 = Z_Malloc(sizeof(FT_Bitmap));
  100. bit2->width = width;
  101. bit2->rows = height;
  102. bit2->pitch = pitch;
  103. bit2->pixel_mode = ft_pixel_mode_grays;
  104. //bit2->pixel_mode = ft_pixel_mode_mono;
  105. bit2->buffer = Z_Malloc(pitch*height);
  106. bit2->num_grays = 256;
  107. Com_Memset( bit2->buffer, 0, size );
  108. FT_Outline_Translate( &glyph->outline, -left, -bottom );
  109. FT_Outline_Get_Bitmap( ftLibrary, &glyph->outline, bit2 );
  110. glyphOut->height = height;
  111. glyphOut->pitch = pitch;
  112. glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1;
  113. glyphOut->bottom = bottom;
  114. return bit2;
  115. }
  116. else {
  117. ri.Printf(PRINT_ALL, "Non-outline fonts are not supported\n");
  118. }
  119. return NULL;
  120. }
  121. void WriteTGA (char *filename, byte *data, int width, int height) {
  122. byte *buffer;
  123. int i, c;
  124. buffer = Z_Malloc(width*height*4 + 18);
  125. Com_Memset (buffer, 0, 18);
  126. buffer[2] = 2; // uncompressed type
  127. buffer[12] = width&255;
  128. buffer[13] = width>>8;
  129. buffer[14] = height&255;
  130. buffer[15] = height>>8;
  131. buffer[16] = 32; // pixel size
  132. // swap rgb to bgr
  133. c = 18 + width * height * 4;
  134. for (i=18 ; i<c ; i+=4)
  135. {
  136. buffer[i] = data[i-18+2]; // blue
  137. buffer[i+1] = data[i-18+1]; // green
  138. buffer[i+2] = data[i-18+0]; // red
  139. buffer[i+3] = data[i-18+3]; // alpha
  140. }
  141. ri.FS_WriteFile(filename, buffer, c);
  142. //f = fopen (filename, "wb");
  143. //fwrite (buffer, 1, c, f);
  144. //fclose (f);
  145. Z_Free (buffer);
  146. }
  147. static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, int *yOut, int *maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight) {
  148. int i;
  149. static glyphInfo_t glyph;
  150. unsigned char *src, *dst;
  151. float scaled_width, scaled_height;
  152. FT_Bitmap *bitmap = NULL;
  153. Com_Memset(&glyph, 0, sizeof(glyphInfo_t));
  154. // make sure everything is here
  155. if (face != NULL) {
  156. FT_Load_Glyph(face, FT_Get_Char_Index( face, c), FT_LOAD_DEFAULT );
  157. bitmap = R_RenderGlyph(face->glyph, &glyph);
  158. if (bitmap) {
  159. glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1;
  160. } else {
  161. return &glyph;
  162. }
  163. if (glyph.height > *maxHeight) {
  164. *maxHeight = glyph.height;
  165. }
  166. if (calcHeight) {
  167. Z_Free(bitmap->buffer);
  168. Z_Free(bitmap);
  169. return &glyph;
  170. }
  171. /*
  172. // need to convert to power of 2 sizes so we do not get
  173. // any scaling from the gl upload
  174. for (scaled_width = 1 ; scaled_width < glyph.pitch ; scaled_width<<=1)
  175. ;
  176. for (scaled_height = 1 ; scaled_height < glyph.height ; scaled_height<<=1)
  177. ;
  178. */
  179. scaled_width = glyph.pitch;
  180. scaled_height = glyph.height;
  181. // we need to make sure we fit
  182. if (*xOut + scaled_width + 1 >= 255) {
  183. if (*yOut + *maxHeight + 1 >= 255) {
  184. *yOut = -1;
  185. *xOut = -1;
  186. Z_Free(bitmap->buffer);
  187. Z_Free(bitmap);
  188. return &glyph;
  189. } else {
  190. *xOut = 0;
  191. *yOut += *maxHeight + 1;
  192. }
  193. } else if (*yOut + *maxHeight + 1 >= 255) {
  194. *yOut = -1;
  195. *xOut = -1;
  196. Z_Free(bitmap->buffer);
  197. Z_Free(bitmap);
  198. return &glyph;
  199. }
  200. src = bitmap->buffer;
  201. dst = imageOut + (*yOut * 256) + *xOut;
  202. if (bitmap->pixel_mode == ft_pixel_mode_mono) {
  203. for (i = 0; i < glyph.height; i++) {
  204. int j;
  205. unsigned char *_src = src;
  206. unsigned char *_dst = dst;
  207. unsigned char mask = 0x80;
  208. unsigned char val = *_src;
  209. for (j = 0; j < glyph.pitch; j++) {
  210. if (mask == 0x80) {
  211. val = *_src++;
  212. }
  213. if (val & mask) {
  214. *_dst = 0xff;
  215. }
  216. mask >>= 1;
  217. if ( mask == 0 ) {
  218. mask = 0x80;
  219. }
  220. _dst++;
  221. }
  222. src += glyph.pitch;
  223. dst += 256;
  224. }
  225. } else {
  226. for (i = 0; i < glyph.height; i++) {
  227. Com_Memcpy(dst, src, glyph.pitch);
  228. src += glyph.pitch;
  229. dst += 256;
  230. }
  231. }
  232. // we now have an 8 bit per pixel grey scale bitmap
  233. // that is width wide and pf->ftSize->metrics.y_ppem tall
  234. glyph.imageHeight = scaled_height;
  235. glyph.imageWidth = scaled_width;
  236. glyph.s = (float)*xOut / 256;
  237. glyph.t = (float)*yOut / 256;
  238. glyph.s2 = glyph.s + (float)scaled_width / 256;
  239. glyph.t2 = glyph.t + (float)scaled_height / 256;
  240. *xOut += scaled_width + 1;
  241. }
  242. Z_Free(bitmap->buffer);
  243. Z_Free(bitmap);
  244. return &glyph;
  245. }
  246. #endif
  247. static int fdOffset;
  248. static byte *fdFile;
  249. int readInt() {
  250. int i = fdFile[fdOffset]+(fdFile[fdOffset+1]<<8)+(fdFile[fdOffset+2]<<16)+(fdFile[fdOffset+3]<<24);
  251. fdOffset += 4;
  252. return i;
  253. }
  254. typedef union {
  255. byte fred[4];
  256. float ffred;
  257. } poor;
  258. float readFloat() {
  259. poor me;
  260. #if idppc
  261. me.fred[0] = fdFile[fdOffset+3];
  262. me.fred[1] = fdFile[fdOffset+2];
  263. me.fred[2] = fdFile[fdOffset+1];
  264. me.fred[3] = fdFile[fdOffset+0];
  265. #else
  266. me.fred[0] = fdFile[fdOffset+0];
  267. me.fred[1] = fdFile[fdOffset+1];
  268. me.fred[2] = fdFile[fdOffset+2];
  269. me.fred[3] = fdFile[fdOffset+3];
  270. #endif
  271. fdOffset += 4;
  272. return me.ffred;
  273. }
  274. void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
  275. #ifdef BUILD_FREETYPE
  276. FT_Face face;
  277. int j, k, xOut, yOut, lastStart, imageNumber;
  278. int scaledSize, newSize, maxHeight, left, satLevels;
  279. unsigned char *out, *imageBuff;
  280. glyphInfo_t *glyph;
  281. image_t *image;
  282. qhandle_t h;
  283. float max;
  284. #endif
  285. void *faceData;
  286. int i, len;
  287. char name[1024];
  288. float dpi = 72; //
  289. float glyphScale = 72.0f / dpi; // change the scale to be relative to 1 based on 72 dpi ( so dpi of 144 means a scale of .5 )
  290. if (pointSize <= 0) {
  291. pointSize = 12;
  292. }
  293. // 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
  294. glyphScale *= 48.0f / pointSize;
  295. // make sure the render thread is stopped
  296. R_SyncRenderThread();
  297. if (registeredFontCount >= MAX_FONTS) {
  298. ri.Printf(PRINT_ALL, "RE_RegisterFont: Too many fonts registered already.\n");
  299. return;
  300. }
  301. Com_sprintf(name, sizeof(name), "fonts/fontImage_%i.dat",pointSize);
  302. for (i = 0; i < registeredFontCount; i++) {
  303. if (Q_stricmp(name, registeredFont[i].name) == 0) {
  304. Com_Memcpy(font, &registeredFont[i], sizeof(fontInfo_t));
  305. return;
  306. }
  307. }
  308. len = ri.FS_ReadFile(name, NULL);
  309. if (len == sizeof(fontInfo_t)) {
  310. ri.FS_ReadFile(name, &faceData);
  311. fdOffset = 0;
  312. fdFile = faceData;
  313. for(i=0; i<GLYPHS_PER_FONT; i++) {
  314. font->glyphs[i].height = readInt();
  315. font->glyphs[i].top = readInt();
  316. font->glyphs[i].bottom = readInt();
  317. font->glyphs[i].pitch = readInt();
  318. font->glyphs[i].xSkip = readInt();
  319. font->glyphs[i].imageWidth = readInt();
  320. font->glyphs[i].imageHeight = readInt();
  321. font->glyphs[i].s = readFloat();
  322. font->glyphs[i].t = readFloat();
  323. font->glyphs[i].s2 = readFloat();
  324. font->glyphs[i].t2 = readFloat();
  325. font->glyphs[i].glyph = readInt();
  326. Com_Memcpy(font->glyphs[i].shaderName, &fdFile[fdOffset], 32);
  327. fdOffset += 32;
  328. }
  329. font->glyphScale = readFloat();
  330. Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH);
  331. // Com_Memcpy(font, faceData, sizeof(fontInfo_t));
  332. Q_strncpyz(font->name, name, sizeof(font->name));
  333. for (i = GLYPH_START; i < GLYPH_END; i++) {
  334. font->glyphs[i].glyph = RE_RegisterShaderNoMip(font->glyphs[i].shaderName);
  335. }
  336. Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
  337. return;
  338. }
  339. #ifndef BUILD_FREETYPE
  340. ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType code not available\n");
  341. #else
  342. if (ftLibrary == NULL) {
  343. ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType not initialized.\n");
  344. return;
  345. }
  346. len = ri.FS_ReadFile(fontName, &faceData);
  347. if (len <= 0) {
  348. ri.Printf(PRINT_ALL, "RE_RegisterFont: Unable to read font file\n");
  349. return;
  350. }
  351. // allocate on the stack first in case we fail
  352. if (FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face )) {
  353. ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, unable to allocate new face.\n");
  354. return;
  355. }
  356. if (FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi)) {
  357. ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, Unable to set face char size.\n");
  358. return;
  359. }
  360. //*font = &registeredFonts[registeredFontCount++];
  361. // make a 256x256 image buffer, once it is full, register it, clean it and keep going
  362. // until all glyphs are rendered
  363. out = Z_Malloc(1024*1024);
  364. if (out == NULL) {
  365. ri.Printf(PRINT_ALL, "RE_RegisterFont: Z_Malloc failure during output image creation.\n");
  366. return;
  367. }
  368. Com_Memset(out, 0, 1024*1024);
  369. maxHeight = 0;
  370. for (i = GLYPH_START; i < GLYPH_END; i++) {
  371. glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue);
  372. }
  373. xOut = 0;
  374. yOut = 0;
  375. i = GLYPH_START;
  376. lastStart = i;
  377. imageNumber = 0;
  378. while ( i <= GLYPH_END ) {
  379. glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse);
  380. if (xOut == -1 || yOut == -1 || i == GLYPH_END) {
  381. // ran out of room
  382. // we need to create an image from the bitmap, set all the handles in the glyphs to this point
  383. //
  384. scaledSize = 256*256;
  385. newSize = scaledSize * 4;
  386. imageBuff = Z_Malloc(newSize);
  387. left = 0;
  388. max = 0;
  389. satLevels = 255;
  390. for ( k = 0; k < (scaledSize) ; k++ ) {
  391. if (max < out[k]) {
  392. max = out[k];
  393. }
  394. }
  395. if (max > 0) {
  396. max = 255/max;
  397. }
  398. for ( k = 0; k < (scaledSize) ; k++ ) {
  399. imageBuff[left++] = 255;
  400. imageBuff[left++] = 255;
  401. imageBuff[left++] = 255;
  402. imageBuff[left++] = ((float)out[k] * max);
  403. }
  404. Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize);
  405. if (r_saveFontData->integer) {
  406. WriteTGA(name, imageBuff, 256, 256);
  407. }
  408. //Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i", imageNumber++, pointSize);
  409. image = R_CreateImage(name, imageBuff, 256, 256, qfalse, qfalse, GL_CLAMP);
  410. h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse);
  411. for (j = lastStart; j < i; j++) {
  412. font->glyphs[j].glyph = h;
  413. Q_strncpyz(font->glyphs[j].shaderName, name, sizeof(font->glyphs[j].shaderName));
  414. }
  415. lastStart = i;
  416. Com_Memset(out, 0, 1024*1024);
  417. xOut = 0;
  418. yOut = 0;
  419. Z_Free(imageBuff);
  420. i++;
  421. } else {
  422. Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t));
  423. i++;
  424. }
  425. }
  426. registeredFont[registeredFontCount].glyphScale = glyphScale;
  427. font->glyphScale = glyphScale;
  428. Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
  429. if (r_saveFontData->integer) {
  430. ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t));
  431. }
  432. Z_Free(out);
  433. ri.FS_FreeFile(faceData);
  434. #endif
  435. }
  436. void R_InitFreeType() {
  437. #ifdef BUILD_FREETYPE
  438. if (FT_Init_FreeType( &ftLibrary )) {
  439. ri.Printf(PRINT_ALL, "R_InitFreeType: Unable to initialize FreeType.\n");
  440. }
  441. #endif
  442. registeredFontCount = 0;
  443. }
  444. void R_DoneFreeType() {
  445. #ifdef BUILD_FREETYPE
  446. if (ftLibrary) {
  447. FT_Done_FreeType( ftLibrary );
  448. ftLibrary = NULL;
  449. }
  450. #endif
  451. registeredFontCount = 0;
  452. }