Skip to content

Commit 1d95548

Browse files
committed
vfont: Add compiler / decompiler ttf <-> vfont from CS:GO
1 parent 51b7169 commit 1d95548

File tree

10 files changed

+385
-0
lines changed

10 files changed

+385
-0
lines changed

common/simplecodec.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===== Copyright c 1996-2008, Valve Corporation, All rights reserved. ======//
2+
//
3+
// Purpose: Simple encoder/decoder
4+
//
5+
//===========================================================================//
6+
7+
#ifndef COMMON_SIMPLE_CODEC
8+
#define COMMON_SIMPLE_CODEC
9+
10+
#include <stdlib.h>
11+
12+
namespace SimpleCodec
13+
{
14+
15+
//
16+
// Encodes the buffer in place
17+
// pvBuffer pointer to the base of the buffer, buffer length is "numBytes + numSultBytes"
18+
// numBytes number of real data bytes already in the buffer
19+
// numSultBytes number of sult bytes to be placed after the buffer data
20+
//
21+
inline void EncodeBuffer( unsigned char *pvBuffer, intp numBytes, unsigned char numSultBytes )
22+
{
23+
unsigned char xx = 0xA7;
24+
25+
// Add some sult in the very end
26+
Assert( numSultBytes > 0 );
27+
pvBuffer[ numBytes + numSultBytes - 1 ] = numSultBytes;
28+
-- numSultBytes;
29+
30+
// Store sult data
31+
for ( unsigned char *pvSult = pvBuffer + numBytes;
32+
numSultBytes -- > 0; ++ pvSult )
33+
{
34+
*pvSult = rand() % 0x100;
35+
xx ^= ( *pvSult + 0xA7 ) % 0x100;
36+
}
37+
38+
// Hash the buffer
39+
for ( ; numBytes -- > 0; ++ pvBuffer )
40+
{
41+
unsigned char v = *pvBuffer;
42+
v ^= xx;
43+
xx = ( v + 0xA7 ) % 0x100;
44+
*pvBuffer = v;
45+
}
46+
}
47+
48+
//
49+
// Decodes the buffer in place
50+
// pvBuffer pointer to the base of the encoded buffer
51+
// numBytes number of buffer bytes on input
52+
// on return contains the number of data bytes decoded (excludes sult)
53+
//
54+
inline void DecodeBuffer( unsigned char *pvBuffer, intp &numBytes )
55+
{
56+
unsigned char xx = 0xA7;
57+
58+
// Discover the number of sult bytes
59+
unsigned char numSultBytes = pvBuffer[ numBytes - 1 ];
60+
numBytes -= numSultBytes;
61+
-- numSultBytes;
62+
63+
// Recover sult data
64+
for ( unsigned char *pvSult = pvBuffer + numBytes;
65+
numSultBytes -- > 0; ++ pvSult )
66+
{
67+
xx ^= ( *pvSult + 0xA7 ) % 0x100;
68+
}
69+
70+
// Hash the buffer
71+
for ( intp numBufBytes = numBytes; numBufBytes -- > 0; ++ pvBuffer )
72+
{
73+
unsigned char v = *pvBuffer;
74+
v ^= xx;
75+
xx = ( *pvBuffer + 0xA7 ) % 0x100;
76+
*pvBuffer = v;
77+
}
78+
}
79+
80+
81+
}; // namespace SimpleCodec
82+
83+
#endif // COMMON_SIMPLE_CODEC

common/valvefont.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===== Copyright c 1996-2008, Valve Corporation, All rights reserved. ======//
2+
//
3+
// Purpose: Simple encoder/decoder
4+
//
5+
//===========================================================================//
6+
7+
#ifndef COMMON_VALVE_FONT
8+
#define COMMON_VALVE_FONT
9+
10+
#include "simplecodec.h"
11+
#include <ctime>
12+
13+
namespace ValveFont
14+
{
15+
16+
//
17+
// Encodes the font buffer, buffer size is increased automatically
18+
//
19+
inline void EncodeFont( CUtlBuffer &buffer )
20+
{
21+
srand( (unsigned) time( NULL ) );
22+
23+
intp numBytes = buffer.TellPut();
24+
int numSultBytes = 0x10 + ( rand() % 0x10 );
25+
26+
char const *szTag = "VFONT1";
27+
intp numTagBytes = V_strlen( szTag );
28+
29+
for ( int k = 0; k < numSultBytes; ++ k )
30+
buffer.PutUnsignedChar( k );
31+
for ( intp k = 0; k < numTagBytes; ++ k )
32+
buffer.PutUnsignedChar( szTag[ k ] );
33+
34+
SimpleCodec::EncodeBuffer( ( unsigned char * ) buffer.Base(), numBytes, numSultBytes );
35+
}
36+
37+
//
38+
// Decodes font buffer, returns true if successful, buffer's put pointer is positioned
39+
// at the end of the font data.
40+
// Returns false on failure.
41+
//
42+
inline bool DecodeFont( CUtlBuffer &buffer )
43+
{
44+
intp numTotalBytes = buffer.TellPut();
45+
46+
char const *szTag = "VFONT1";
47+
intp numTagBytes = V_strlen( szTag );
48+
49+
if ( numTotalBytes <= numTagBytes )
50+
return false;
51+
52+
if ( memcmp( (( unsigned char * ) buffer.Base() ) + numTotalBytes - numTagBytes,
53+
szTag, numTagBytes ) )
54+
return false;
55+
56+
numTotalBytes -= numTagBytes;
57+
SimpleCodec::DecodeBuffer( ( unsigned char * ) buffer.Base(), numTotalBytes );
58+
59+
buffer.SeekPut( CUtlBuffer::SEEK_HEAD, numTotalBytes );
60+
return true;
61+
}
62+
63+
}; // namespace ValveFont
64+
65+
#endif // COMMON_VALVE_FONT
66+

utils/vfont/resource.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//========= Copyright Valve Corporation, All rights reserved. ============//
2+
3+
#define SRC_IDI_APP_MAIN 101
4+
5+
#define SRC_PRODUCT_FILE_DESCRIPTION_STRING "Valve .ttf font to .vfont compiler / decompiler"

utils/vfont/resources/app.ico

162 KB
Binary file not shown.

utils/vfont/vfont.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Valve Corporation, All rights reserved.
2+
//
3+
// Purpose: Valve font compiler.
4+
5+
#include <cstdlib>
6+
#include <cstdio>
7+
8+
#include "tier1/utlbuffer.h"
9+
#include "valvefont.h"
10+
11+
int Usage() {
12+
fprintf(stderr, "Usage: vfont inputfont.ttf [outputfont.vfont]\n");
13+
return -1;
14+
}
15+
16+
int main(int argc, char **argv) {
17+
#ifdef PLATFORM_64BITS
18+
printf("Valve Software - vfont.exe [64 bit] (" __DATE__ ")\n");
19+
#else
20+
printf("Valve Software - vfont.exe (" __DATE__ ")\n");
21+
#endif
22+
23+
int iArg = 1;
24+
if (iArg >= argc) return Usage();
25+
26+
//
27+
// Check if we are in decompiler mode
28+
//
29+
#if VFONT_DECOMPILER
30+
bool bDecompiler = !stricmp(argv[iArg], "-d");
31+
if (bDecompiler) ++iArg;
32+
#endif
33+
34+
if (iArg >= argc) return Usage();
35+
36+
//
37+
// Input file
38+
//
39+
char const *szInput = argv[iArg];
40+
++iArg;
41+
42+
//
43+
// Output file
44+
//
45+
char szOutput[512] = {0};
46+
47+
if (iArg >= argc) {
48+
intp numBytes = V_strlen(szInput);
49+
V_strcpy_safe(szOutput, szInput);
50+
51+
char *pDot = strrchr(szOutput, '.');
52+
char *pSlash1 = strrchr(szOutput, '/');
53+
char *pSlash2 = strrchr(szOutput, '\\');
54+
55+
if (!pDot) {
56+
pDot = szOutput + numBytes;
57+
} else if (pDot < pSlash1 || pDot < pSlash2) {
58+
pDot = szOutput + numBytes;
59+
}
60+
61+
sprintf(pDot, ".vfont");
62+
} else {
63+
V_strcpy_safe(szOutput, argv[iArg]);
64+
}
65+
66+
//
67+
// Read the input
68+
//
69+
FILE *fin = fopen(szInput, "rb");
70+
if (!fin) {
71+
fprintf(stderr, "Error: cannot open input file '%s'!\n", szInput);
72+
return -2;
73+
}
74+
75+
fseek(fin, 0, SEEK_END);
76+
long lSize = ftell(fin);
77+
fseek(fin, 0, SEEK_SET);
78+
79+
CUtlBuffer buf;
80+
buf.EnsureCapacity(lSize);
81+
fread(buf.Base(), 1, lSize, fin);
82+
buf.SeekPut(CUtlBuffer::SEEK_HEAD, lSize);
83+
fclose(fin);
84+
85+
//
86+
// Compile
87+
//
88+
#if VFONT_DECOMPILER
89+
if (bDecompiler) {
90+
bool bResult = ValveFont::DecodeFont(buf);
91+
if (!bResult) {
92+
fprintf(stderr, "Error: cannot decompile input file '%s'!\n", szInput);
93+
return 1;
94+
}
95+
} else
96+
ValveFont::EncodeFont(buf);
97+
#else
98+
ValveFont::EncodeFont(buf);
99+
#endif
100+
101+
//
102+
// Write the output
103+
//
104+
FILE *fout = fopen(szOutput, "wb");
105+
if (!fout) {
106+
fprintf(stderr, "Error: cannot open output file '%s'!\n", szOutput);
107+
return -3;
108+
}
109+
110+
fwrite(buf.Base(), 1, buf.TellPut(), fout);
111+
fclose(fout);
112+
113+
printf("vfont successfully %scompiled '%s' as '%s'.\n",
114+
#if VFONT_DECOMPILER
115+
bDecompiler ? "de" : "",
116+
#else
117+
"",
118+
#endif
119+
szInput, szOutput);
120+
return 0;
121+
}

utils/vfont/vfont.vpc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//-----------------------------------------------------------------------------
2+
// uvlightmap.VPC
3+
//
4+
// Project Script
5+
//-----------------------------------------------------------------------------
6+
7+
$Macro SRCDIR "..\.."
8+
$Macro OUTBINDIR "$SRCDIR\..\game\bin"
9+
10+
$Include "$SRCDIR\vpc_scripts\source_exe_con_base.vpc"
11+
12+
$Configuration
13+
{
14+
$Compiler
15+
{
16+
$AdditionalIncludeDirectories "$BASE,..\common"
17+
// $PreprocessorDefinitions "$BASE"
18+
}
19+
20+
$Linker
21+
{
22+
$AdditionalDependencies "winmm.lib comctl32.lib"
23+
// $IgnoreSpecificLibrary "libc;libcd;libcmtd;libcp"
24+
// $AdditionalOptions "/FORCE"
25+
}
26+
}
27+
28+
$Project "vfont"
29+
{
30+
$Folder "Source Files"
31+
{
32+
$File "vfont.cpp"
33+
}
34+
35+
$Folder "Header Files"
36+
{
37+
}
38+
39+
$Folder "Link Libraries"
40+
{
41+
$Lib tier2
42+
$Lib tier3
43+
}
44+
}

utils/vfont/vfont_decompiler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright Valve Corporation, All rights reserved.
2+
//
3+
// Valve font compiler and decompiler
4+
5+
//
6+
// NOTE: This tool can be used to *decompile* valve font files.
7+
//
8+
#define VFONT_DECOMPILER 1
9+
10+
#include "vfont.cpp"

utils/vfont/vfont_decompiler.vpc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//-----------------------------------------------------------------------------
2+
// uvlightmap.VPC
3+
//
4+
// Project Script
5+
//-----------------------------------------------------------------------------
6+
7+
$Macro SRCDIR "..\.."
8+
$Macro OUTBINDIR "$SRCDIR\..\game\bin"
9+
10+
$Include "$SRCDIR\vpc_scripts\source_exe_con_base.vpc"
11+
12+
$Configuration
13+
{
14+
$Compiler
15+
{
16+
$AdditionalIncludeDirectories "$BASE,..\common"
17+
// $PreprocessorDefinitions "$BASE"
18+
}
19+
20+
$Linker
21+
{
22+
$AdditionalDependencies "winmm.lib comctl32.lib"
23+
// $IgnoreSpecificLibrary "libc;libcd;libcmtd;libcp"
24+
// $AdditionalOptions "/FORCE"
25+
}
26+
}
27+
28+
$Project "vfont_decompiler"
29+
{
30+
$Folder "Source Files"
31+
{
32+
$File "vfont_decompiler.cpp"
33+
}
34+
35+
$Folder "Header Files"
36+
{
37+
}
38+
39+
$Folder "Link Libraries"
40+
{
41+
$Lib tier2
42+
$Lib tier3
43+
}
44+
}

vpc_scripts/groups.vgc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ $Group "hammer"
271271
"tier3"
272272
"vbsp"
273273
"vbspinfo"
274+
"vfont"
275+
"vfont_decompiler"
274276
"vgui_controls"
275277
"vgui_dll"
276278
"vgui_surfacelib"

0 commit comments

Comments
 (0)