Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/SupportedAssetTypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using `Linker`):
| MapEnts | ✅ | ❌ | |
| GfxWorld | ❌ | ❌ | |
| GfxLightDef | ✅ | ✅ | |
| Font_s | | | |
| Font_s | | | |
| MenuList | ❌ | ❌ | |
| menuDef_t | ❌ | ❌ | |
| LocalizeEntry | ✅ | ✅ | |
Expand Down Expand Up @@ -63,7 +63,7 @@ using `Linker`):
| FxWorld | ❌ | ❌ | |
| GfxWorld | ❌ | ❌ | |
| GfxLightDef | ✅ | ✅ | |
| Font_s | | | |
| Font_s | | | |
| MenuList | ✅ | ✅ | The output is decompiled. The result will not be the same as the input. |
| menuDef_t | ✅ | ✅ | See menulist. |
| LocalizeEntry | ✅ | ✅ | |
Expand Down Expand Up @@ -105,7 +105,7 @@ using `Linker`):
| FxWorld | ❌ | ❌ | |
| GfxWorld | ❌ | ❌ | |
| GfxLightDef | ✅ | ✅ | |
| Font_s | | | |
| Font_s | | | |
| MenuList | ✅ | ✅ | The output is decompiled. The result will not be the same as the input. |
| menuDef_t | ✅ | ✅ | See menulist. |
| LocalizeEntry | ✅ | ✅ | |
Expand Down Expand Up @@ -145,7 +145,7 @@ using `Linker`):
| MapEnts | ✅ | ❌ | |
| GfxWorld | ❌ | ❌ | |
| GfxLightDef | ❌ | ❌ | |
| Font_s | | | |
| Font_s | | | |
| MenuList | ❌ | ❌ | |
| menuDef_t | ❌ | ❌ | |
| LocalizeEntry | ✅ | ❌ | |
Expand Down Expand Up @@ -177,7 +177,7 @@ using `Linker`):
| MapEnts | ❌ | ❌ | |
| GfxWorld | ❌ | ❌ | |
| GfxLightDef | ✅ | ✅ | |
| Font_s | | | |
| Font_s | | | |
| MenuList | ❌ | ❌ | |
| menuDef_t | ❌ | ❌ | |
| LocalizeEntry | ✅ | ✅ | |
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Game/IW3/IW3_Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -2455,9 +2455,9 @@ namespace IW3
uint16_t letter;
char x0;
char y0;
char dx;
char pixelWidth;
char pixelHeight;
unsigned char dx;
unsigned char pixelWidth;
unsigned char pixelHeight;
float s0;
float t0;
float s1;
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Game/IW4/IW4_Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -2676,9 +2676,9 @@ namespace IW4
uint16_t letter;
char x0;
char y0;
char dx;
char pixelWidth;
char pixelHeight;
unsigned char dx;
unsigned char pixelWidth;
unsigned char pixelHeight;
float s0;
float t0;
float s1;
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Game/T4/T4_Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -2711,9 +2711,9 @@ namespace T4
uint16_t letter;
char x0;
char y0;
char dx;
char pixelWidth;
char pixelHeight;
unsigned char dx;
unsigned char pixelWidth;
unsigned char pixelHeight;
float s0;
float t0;
float s1;
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Game/T5/T5_Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -3310,9 +3310,9 @@ namespace T5
uint16_t letter;
char x0;
char y0;
char dx;
char pixelWidth;
char pixelHeight;
unsigned char dx;
unsigned char pixelWidth;
unsigned char pixelHeight;
float s0;
float t0;
float s1;
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Game/T6/T6_Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -3997,9 +3997,9 @@ namespace T6
uint16_t letter;
char x0;
char y0;
char dx;
char pixelWidth;
char pixelHeight;
unsigned char dx;
unsigned char pixelWidth;
unsigned char pixelHeight;
float s0;
float t0;
float s1;
Expand Down
107 changes: 107 additions & 0 deletions src/ObjCommon/Font/FontCommon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "FontCommon.h"

#include <filesystem>

namespace fs = std::filesystem;

namespace font
{
std::string GetJsonFileNameForAssetName(const std::string& assetName)
{
fs::path path(assetName);
path.replace_extension(".json");
return path.string();
}

bool IsPrintableLetter(const unsigned letter)
{
// Control characters
if (letter < 0x20)
return false;

// Printable ascii characters
if (letter < 0x7F)
return true;

// There are characters after this point that are printable as well
// But they don't seem to play a role in cod fonts
if (letter > 0xFF)
return false;

switch (letter)
{
case 0x7F:
case 0x81:
case 0x8D:
case 0x8F:
case 0x90:
case 0x9D:
case 0xA0:
case 0xAD:
return false;
default:
return true;
}
}

std::string LetterToString(const unsigned letter)
{
// UTF-8 => 1 byte
if (letter < 0x80)
return std::string(1, static_cast<char>(letter));

if (letter < 0x800)
{
// UTF-8 => 2 bytes
std::string s(2, '\0');
s[0] = static_cast<char>(0xC0 | (letter >> 6u));
s[1] = static_cast<char>(0x80 | (letter & 0x3F));

return s;
}

// UTF-8 => 3 bytes
std::string s(3, '\0');
s[0] = static_cast<char>(0xE0 | (letter >> 12u));
s[1] = static_cast<char>(0x80 | ((letter >> 6u) & 0x3F));
s[2] = static_cast<char>(0x80 | (letter & 0x3F));

return s;
}

unsigned StringToLetter(const std::string& str)
{
const auto strSize = str.size();
if (strSize < 1)
return 0;

const auto firstByte = static_cast<unsigned char>(str[0]);

// UTF-8 => 1 byte
if ((firstByte & 0x80) == 0)
return firstByte;

if (strSize < 2)
return 0;
const auto secondByte = static_cast<unsigned char>(str[1]);

if ((firstByte & 0xE0) == 0xC0 && (secondByte & 0xC0) == 0x80)
{
// UTF-8 => 2 bytes
return ((firstByte & 0x1F) << 6u) | (secondByte & 0x3F);
}

if (strSize < 3)
return 0;
const auto thirdByte = static_cast<unsigned char>(str[2]);

if ((firstByte & 0xF0) == 0xE0 && (secondByte & 0xC0) == 0x80 && (thirdByte & 0xC0) == 0x80)
{
// UTF-8 => 3 bytes
return ((firstByte & 0x0F) << 12u) | ((secondByte & 0x3F) << 6u) | (thirdByte & 0x3F);
}

// Unsupported
return 0;
}
} // namespace font
13 changes: 13 additions & 0 deletions src/ObjCommon/Font/FontCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>

namespace font
{
std::string GetJsonFileNameForAssetName(const std::string& assetName);

bool IsPrintableLetter(unsigned letter);

std::string LetterToString(unsigned letter);
unsigned StringToLetter(const std::string& str);
} // namespace font
67 changes: 67 additions & 0 deletions src/ObjCommon/Font/JsonFont.h.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#options GAME (IW3, IW4, IW5, T4, T5)

#filename "Game/" + GAME + "/Font/JsonFont" + GAME + ".h"

// This file was templated.
// See JsonFont.h.template.
// Do not modify, changes will be lost.

#pragma once

#include "Json/JsonExtension.h"
#include "Font/FontCommon.h"

#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>

namespace GAME
{
class JsonGlyph
{
public:
unsigned letter;
int x0;
int y0;
unsigned dx;
unsigned pixelWidth;
unsigned pixelHeight;
float s0;
float t0;
float s1;
float t1;
};

NLOHMANN_TO_JSON_METHOD(JsonGlyph)
{
if (font::IsPrintableLetter(nlohmann_json_t.letter))
extended_to_json("letter", nlohmann_json_j, font::LetterToString(nlohmann_json_t.letter));
else
extended_to_json("letter", nlohmann_json_j, nlohmann_json_t.letter);

NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_TO, x0, y0, dx, pixelWidth, pixelHeight, s0, t0, s1, t1))
}

NLOHMANN_FROM_JSON_METHOD(JsonGlyph)
{
const auto jLetter = nlohmann_json_j.at("letter");
if (jLetter.type() == BasicJsonType::value_t::string)
nlohmann_json_t.letter = font::StringToLetter(jLetter.template get<std::string>());
else
nlohmann_json_t.letter = jLetter.template get<unsigned>();

NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_FROM, x0, y0, dx, pixelWidth, pixelHeight, s0, t0, s1, t1))
}

class JsonFont
{
public:
unsigned pixelHeight;
std::string material;
std::optional<std::string> glowMaterial;
std::vector<JsonGlyph> glyphs;
};

NLOHMANN_DEFINE_TYPE_EXTENSION(JsonFont, pixelHeight, material, glowMaterial, glyphs);
} // namespace GAME
Loading