/*
===========================================================================
IceTech GPL Source Code
Copyright (C) 2026 Justin Marshall
This file is part of the IceTech GPL Source Code ("IceTech Source Code").
IceTech Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
IceTech Source Code 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with IceTech Source Code. If not, see .
In addition, the IceTech 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 IceTech Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing Justin Marshall, justinmarshall20@gmail.com
===========================================================================
*/
#include "precompiled.h"
#include "tr_local.h"
#define FILESIZE_fontInfo_t (20548)
#ifdef BUILD_FREETYPE
#include "../ft2/fterrors.h"
#include "../ft2/ftsystem.h"
#include "../ft2/ftimage.h"
#include "../ft2/freetype.h"
#include "../ft2/ftoutln.h"
#define _FLOOR(x) ((x) & -64)
#define _CEIL(x) (((x)+63) & -64)
#define _TRUNC(x) ((x) >> 6)
FT_Library ftLibrary = NULL;
#endif
#ifdef BUILD_FREETYPE
/*
============
R_GetGlyphInfo
============
*/
void R_GetGlyphInfo(FT_GlyphSlot glyph, int* left, int* right, int* width, int* top, int* bottom, int* height, int* pitch) {
*left = _FLOOR(glyph->metrics.horiBearingX);
*right = _CEIL(glyph->metrics.horiBearingX + glyph->metrics.width);
*width = _TRUNC(*right - *left);
*top = _CEIL(glyph->metrics.horiBearingY);
*bottom = _FLOOR(glyph->metrics.horiBearingY - glyph->metrics.height);
*height = _TRUNC(*top - *bottom);
*pitch = (qtrue ? (*width + 3) & -4 : (*width + 7) >> 3);
}
/*
============
R_RenderGlyph
============
*/
FT_Bitmap* R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
FT_Bitmap* bit2;
int left, right, width, top, bottom, height, pitch, size;
R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch);
if (glyph->format == ft_glyph_format_outline) {
size = pitch * height;
bit2 = Mem_Alloc(sizeof(FT_Bitmap));
bit2->width = width;
bit2->rows = height;
bit2->pitch = pitch;
bit2->pixel_mode = ft_pixel_mode_grays;
//bit2->pixel_mode = ft_pixel_mode_mono;
bit2->buffer = Mem_Alloc(pitch * height);
bit2->num_grays = 256;
memset(bit2->buffer, 0, size);
FT_Outline_Translate(&glyph->outline, -left, -bottom);
FT_Outline_Get_Bitmap(ftLibrary, &glyph->outline, bit2);
glyphOut->height = height;
glyphOut->pitch = pitch;
glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1;
glyphOut->bottom = bottom;
return bit2;
}
else {
common->Printf("Non-outline fonts are not supported\n");
}
return NULL;
}
/*
============
RE_ConstructGlyphInfo
============
*/
glyphInfo_t* RE_ConstructGlyphInfo(unsigned char* imageOut, int* xOut, int* yOut, int* maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight) {
int i;
static glyphInfo_t glyph;
unsigned char* src, * dst;
float scaled_width, scaled_height;
FT_Bitmap* bitmap = NULL;
memset(&glyph, 0, sizeof(glyphInfo_t));
// make sure everything is here
if (face != NULL) {
FT_Load_Glyph(face, FT_Get_Char_Index(face, c), FT_LOAD_DEFAULT);
bitmap = R_RenderGlyph(face->glyph, &glyph);
if (bitmap) {
glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1;
}
else {
return &glyph;
}
if (glyph.height > *maxHeight) {
*maxHeight = glyph.height;
}
if (calcHeight) {
Mem_Free(bitmap->buffer);
Mem_Free(bitmap);
return &glyph;
}
/*
// need to convert to power of 2 sizes so we do not get
// any scaling from the gl upload
for (scaled_width = 1 ; scaled_width < glyph.pitch ; scaled_width<<=1)
;
for (scaled_height = 1 ; scaled_height < glyph.height ; scaled_height<<=1)
;
*/
scaled_width = glyph.pitch;
scaled_height = glyph.height;
// we need to make sure we fit
if (*xOut + scaled_width + 1 >= 255) {
if (*yOut + *maxHeight + 1 >= 255) {
*yOut = -1;
*xOut = -1;
Mem_Free(bitmap->buffer);
Mem_Free(bitmap);
return &glyph;
}
else {
*xOut = 0;
*yOut += *maxHeight + 1;
}
}
else if (*yOut + *maxHeight + 1 >= 255) {
*yOut = -1;
*xOut = -1;
Mem_Free(bitmap->buffer);
Mem_Free(bitmap);
return &glyph;
}
src = bitmap->buffer;
dst = imageOut + (*yOut * 256) + *xOut;
if (bitmap->pixel_mode == ft_pixel_mode_mono) {
for (i = 0; i < glyph.height; i++) {
int j;
unsigned char* _src = src;
unsigned char* _dst = dst;
unsigned char mask = 0x80;
unsigned char val = *_src;
for (j = 0; j < glyph.pitch; j++) {
if (mask == 0x80) {
val = *_src++;
}
if (val & mask) {
*_dst = 0xff;
}
mask >>= 1;
if (mask == 0) {
mask = 0x80;
}
_dst++;
}
src += glyph.pitch;
dst += 256;
}
}
else {
for (i = 0; i < glyph.height; i++) {
memcpy(dst, src, glyph.pitch);
src += glyph.pitch;
dst += 256;
}
}
// we now have an 8 bit per pixel grey scale bitmap
// that is width wide and pf->ftSize->metrics.y_ppem tall
glyph.imageHeight = scaled_height;
glyph.imageWidth = scaled_width;
glyph.s = (float)*xOut / 256;
glyph.t = (float)*yOut / 256;
glyph.s2 = glyph.s + (float)scaled_width / 256;
glyph.t2 = glyph.t + (float)scaled_height / 256;
*xOut += scaled_width + 1;
}
Mem_Free(bitmap->buffer);
Mem_Free(bitmap);
return &glyph;
}
#endif
static int fdOffset;
static byte* fdFile;
/*
============
readInt
============
*/
int readInt(void) {
int i = fdFile[fdOffset] + (fdFile[fdOffset + 1] << 8) + (fdFile[fdOffset + 2] << 16) + (fdFile[fdOffset + 3] << 24);
fdOffset += 4;
return i;
}
/*
============
readFloat
============
*/
float readFloat(void) {
float f;
memcpy(&f, &fdFile[fdOffset], sizeof(float));
fdOffset += sizeof(float);
return LittleFloat(f);
}
/*
============
RegisterFont
Loads 3 point sizes, 12, 24, and 48
============
*/
bool idRenderSystemLocal::RegisterFont(const char* fontName, fontInfoEx_t& font) {
#ifdef QUAKE4
void* faceData;
ID_TIME_T ftime;
int fontCount;
int found;
char fontDatName[1024];
char materialName[1024];
memset(&font, 0, sizeof(font));
found = 0;
for (fontCount = 0; fontCount < 3; fontCount++) {
int pointSize;
fontInfo_t* outFont;
if (fontCount == 0) {
pointSize = 12;
outFont = &font.fontInfoSmall;
}
else if (fontCount == 1) {
pointSize = 24;
outFont = &font.fontInfoMedium;
}
else {
pointSize = 48;
outFont = &font.fontInfoLarge;
}
idStr::snPrintf(fontDatName, sizeof(fontDatName), "%s_%i.fontdat", fontName, pointSize);
#define FILESIZE_q4FontInfo_t 9236
if (fileSystem->ReadFile(fontDatName, NULL, &ftime) != FILESIZE_q4FontInfo_t) {
continue;
}
if (fileSystem->ReadFile(fontDatName, &faceData, &ftime) <= 0 || faceData == NULL) {
continue;
}
fdOffset = 0;
fdFile = reinterpret_cast(faceData);
found |= 1 << fontCount;
idStr::Copynz(outFont->name, fontDatName, sizeof(outFont->name));
int mw = 0;
int mh = 0;
for (int i = 0; i < GLYPHS_PER_FONT; i++) {
glyphInfo_t& glyph = outFont->glyphs[i];
/*
Quake 4 disk glyph order:
float width;
float height;
float horiAdvance;
float horiBearingX;
float horiBearingY;
float s1;
float t1;
float s2;
float t2;
Doom 3 glyphInfo_t is not the same struct, so map it.
*/
const float q4Width = readFloat();
const float q4Height = readFloat();
const float q4HoriAdvance = readFloat();
const float q4HoriBearingX = readFloat();
const float q4HoriBearingY = readFloat();
const float q4S1 = readFloat();
const float q4T1 = readFloat();
const float q4S2 = readFloat();
const float q4T2 = readFloat();
glyph.height = (int)q4Height;
glyph.top = (int)q4HoriBearingY;
glyph.bottom = (int)(q4HoriBearingY - q4Height);
glyph.pitch = (int)q4Width;
glyph.xSkip = (int)q4HoriAdvance;
glyph.imageWidth = (int)q4Width;
glyph.imageHeight = (int)q4Height;
glyph.s = q4S1;
glyph.t = q4T1;
glyph.s2 = q4S2;
glyph.t2 = q4T2;
glyph.glyph = NULL;
glyph.shaderName[0] = '\0';
if (mw < glyph.xSkip) {
mw = glyph.xSkip;
}
if (mh < glyph.height) {
mh = glyph.height;
}
}
/*
Quake 4 stores these after the glyph array:
pointSize, fontHeight, ascender, descender, material pointer
Doom 3's fontInfo_t does not have those fields, so consume them.
*/
const float q4PointSize = readFloat();
const float q4FontHeight = readFloat();
const float q4Ascender = readFloat();
const float q4Descender = readFloat();
// consume the saved material pointer / padding from the .fontdat
readInt();
outFont->glyphScale = 48.0f / pointSize;
fileSystem->FreeFile(faceData);
faceData = NULL;
/*
The Quake 4 atlas is the .tga beside the .fontdat:
fonts/marine_12.fontdat
fonts/marine_12.tga
Doom 3 stores material per glyph, so assign the same atlas material
to every glyph.
*/
idStr::snPrintf(materialName, sizeof(materialName), "%s_%i.tga", fontName, pointSize);
for (int i = 0; i < GLYPHS_PER_FONT; i++) {
outFont->glyphs[i].glyph = declManager->FindMaterial(materialName);
if (outFont->glyphs[i].glyph != NULL) {
outFont->glyphs[i].glyph->SetSort(SS_GUI);
}
idStr::Copynz(outFont->glyphs[i].shaderName, materialName, sizeof(outFont->glyphs[i].shaderName));
}
if (fontCount == 0) {
font.maxWidthSmall = mw;
font.maxHeightSmall = mh;
}
else if (fontCount == 1) {
font.maxWidthMedium = mw;
font.maxHeightMedium = mh;
}
else {
font.maxWidthLarge = mw;
font.maxHeightLarge = mh;
}
}
switch (found) {
case 1:
memcpy(&font.fontInfoMedium, &font.fontInfoSmall, sizeof(font.fontInfoMedium));
font.maxWidthMedium = font.maxWidthSmall;
font.maxHeightMedium = font.maxHeightSmall;
memcpy(&font.fontInfoLarge, &font.fontInfoSmall, sizeof(font.fontInfoLarge));
font.maxWidthLarge = font.maxWidthSmall;
font.maxHeightLarge = font.maxHeightSmall;
break;
case 2:
memcpy(&font.fontInfoSmall, &font.fontInfoMedium, sizeof(font.fontInfoSmall));
font.maxWidthSmall = font.maxWidthMedium;
font.maxHeightSmall = font.maxHeightMedium;
memcpy(&font.fontInfoLarge, &font.fontInfoMedium, sizeof(font.fontInfoLarge));
font.maxWidthLarge = font.maxWidthMedium;
font.maxHeightLarge = font.maxHeightMedium;
break;
case 3:
memcpy(&font.fontInfoLarge, &font.fontInfoMedium, sizeof(font.fontInfoLarge));
font.maxWidthLarge = font.maxWidthMedium;
font.maxHeightLarge = font.maxHeightMedium;
break;
case 4:
memcpy(&font.fontInfoSmall, &font.fontInfoLarge, sizeof(font.fontInfoSmall));
font.maxWidthSmall = font.maxWidthLarge;
font.maxHeightSmall = font.maxHeightLarge;
memcpy(&font.fontInfoMedium, &font.fontInfoLarge, sizeof(font.fontInfoMedium));
font.maxWidthMedium = font.maxWidthLarge;
font.maxHeightMedium = font.maxHeightLarge;
break;
case 5:
memcpy(&font.fontInfoMedium, &font.fontInfoLarge, sizeof(font.fontInfoMedium));
font.maxWidthMedium = font.maxWidthLarge;
font.maxHeightMedium = font.maxHeightLarge;
break;
case 6:
memcpy(&font.fontInfoSmall, &font.fontInfoMedium, sizeof(font.fontInfoSmall));
font.maxWidthSmall = font.maxWidthMedium;
font.maxHeightSmall = font.maxHeightMedium;
break;
default:
break;
}
return found != 0;
#else
#ifdef BUILD_FREETYPE
FT_Face face;
int j, k, xOut, yOut, lastStart, imageNumber;
int scaledSize, newSize, maxHeight, left, satLevels;
unsigned char* out, * imageBuff;
glyphInfo_t* glyph;
idImage* image;
idMaterial* h;
float max;
#endif
void* faceData;
ID_TIME_T ftime;
int i, len, fontCount;
char name[1024];
int pointSize = 12;
assert(sizeof(float) == 4 && "if this is false, readFloat() won't work");
memset(&font, 0, sizeof(font));
for (fontCount = 0; fontCount < 3; fontCount++) {
if (fontCount == 0) {
pointSize = 12;
}
else if (fontCount == 1) {
pointSize = 24;
}
else {
pointSize = 48;
}
float glyphScale = 1.0f;
glyphScale *= 48.0f / pointSize;
idStr::snPrintf(name, sizeof(name), "%s/fontImage_%i.dat", fontName, pointSize);
fontInfo_t* outFont;
if (fontCount == 0) {
outFont = &font.fontInfoSmall;
}
else if (fontCount == 1) {
outFont = &font.fontInfoMedium;
}
else {
outFont = &font.fontInfoLarge;
}
idStr::Copynz(outFont->name, name, sizeof(outFont->name));
len = fileSystem->ReadFile(name, NULL, &ftime);
if (len != FILESIZE_fontInfo_t) {
common->Warning("RegisterFont: couldn't find font: '%s'", name);
return false;
}
fileSystem->ReadFile(name, &faceData, &ftime);
fdOffset = 0;
fdFile = reinterpret_cast(faceData);
for (i = 0; i < GLYPHS_PER_FONT; i++) {
outFont->glyphs[i].height = readInt();
outFont->glyphs[i].top = readInt();
outFont->glyphs[i].bottom = readInt();
outFont->glyphs[i].pitch = readInt();
outFont->glyphs[i].xSkip = readInt();
outFont->glyphs[i].imageWidth = readInt();
outFont->glyphs[i].imageHeight = readInt();
outFont->glyphs[i].s = readFloat();
outFont->glyphs[i].t = readFloat();
outFont->glyphs[i].s2 = readFloat();
outFont->glyphs[i].t2 = readFloat();
readInt();
memcpy(outFont->glyphs[i].shaderName, &fdFile[fdOffset + 6], 32 - 6);
fdOffset += 32;
}
outFont->glyphScale = readFloat();
int mw = 0;
int mh = 0;
for (i = GLYPH_START; i < GLYPH_END; i++) {
idStr::snPrintf(name, sizeof(name), "%s/%s", fontName, outFont->glyphs[i].shaderName);
outFont->glyphs[i].glyph = declManager->FindMaterial(name);
outFont->glyphs[i].glyph->SetSort(SS_GUI);
if (mh < outFont->glyphs[i].height) {
mh = outFont->glyphs[i].height;
}
if (mw < outFont->glyphs[i].xSkip) {
mw = outFont->glyphs[i].xSkip;
}
}
if (fontCount == 0) {
font.maxWidthSmall = mw;
font.maxHeightSmall = mh;
}
else if (fontCount == 1) {
font.maxWidthMedium = mw;
font.maxHeightMedium = mh;
}
else {
font.maxWidthLarge = mw;
font.maxHeightLarge = mh;
}
fileSystem->FreeFile(faceData);
}
return true;
#endif // QUAKE4
}
/*
============
R_InitFreeType
============
*/
void R_InitFreeType(void) {
#ifdef BUILD_FREETYPE
if (FT_Init_FreeType(&ftLibrary)) {
common->Printf("R_InitFreeType: Unable to initialize FreeType.\n");
}
#endif
// registeredFontCount = 0;
}
/*
============
R_DoneFreeType
============
*/
void R_DoneFreeType(void) {
#ifdef BUILD_FREETYPE
if (ftLibrary) {
FT_Done_FreeType(ftLibrary);
ftLibrary = NULL;
}
#endif
// registeredFontCount = 0;
}