jak-project/common/util/FontUtils.h
ManDude 18ddd1613c
Jak 2 pc subtitle support (#2672)
Adds support for adding custom subtitles to Jak 2 audio. Comes with a
new editor for the new system and format. Compared to the Jak 1 system,
this is much simpler to make an editor for.

Comes with a few subtitles already made as an example.
Cutscenes are not officially supported but you can technically subtitle
those with editor, so please don't right now.

This new system supports multiple subtitles playing at once (even from a
single source!) and will smartly push the subtitles up if there's a
message already playing:

![image](https://github.com/open-goal/jak-project/assets/7569514/033e6374-a05a-4c31-b029-51868153a932)

![image](https://github.com/open-goal/jak-project/assets/7569514/5298aa6d-a183-446e-bdb6-61c4682df917)

Unlike in Jak 1, it will not hide the bottom HUD when subtitles are
active:

![image](https://github.com/open-goal/jak-project/assets/7569514/d466bfc0-55d0-4689-a6e1-b7784b9fff59)

Sadly this leaves us with not much space for the subtitle region (and
the subtitles are shrunk when the minimap is enabled) but when you have
guards and citizens talking all the time, hiding the HUD every time
anyone spoke would get really frustrating.

The subtitle speaker is also color-coded now, because I thought that
would be fun to do.

TODO:
- [x] proper cutscene support.
- [x] merge mode for cutscenes so we don't have to rewrite the script?

---------

Co-authored-by: Hat Kid <6624576+Hat-Kid@users.noreply.github.com>
2023-06-08 01:04:16 +01:00

94 lines
3.1 KiB
C++

#pragma once
/*!
* @file FontUtils.h
*
* Code for handling text and strings in Jak 1's "large font" format.
*
* MAKE SURE THIS FILE IS ENCODED IN UTF-8!!! The various strings here depend on it.
* Always verify the encoding if string detection suddenly goes awry.
*/
#include <map>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "common/common_types.h"
// version of the game text file's text encoding. Not real, but we need to differentiate them
// somehow, since the encoding changes.
enum class GameTextVersion {
JAK1_V1 = 10, // jak 1 (ntsc-u v1)
JAK1_V2 = 11, // jak 1 (pal+)
JAK2 = 20, // jak 2
JAK3 = 30, // jak 3
JAKX = 40 // jak x
};
extern const std::unordered_map<std::string, GameTextVersion> sTextVerEnumMap;
const std::string& get_text_version_name(GameTextVersion version);
GameTextVersion get_text_version_from_name(const std::string& name);
/*!
* What bytes a set of characters (UTF-8) correspond to. You can convert to and fro.
*/
struct EncodeInfo {
std::string chars;
std::vector<u8> bytes;
};
/*!
* Replace an unconventional string of characters with/from something more readable.
* For example, turns Ñ into N + ~ + a bunch of modifiers.
*/
struct ReplaceInfo {
std::string from;
std::string to;
};
/*!
* All the information to convert UTF-8 text into game text.
*/
class GameTextFontBank {
GameTextVersion m_version; // the version of the game text. we determine this ourselves.
std::vector<EncodeInfo>* m_encode_info;
std::vector<ReplaceInfo>* m_replace_info;
std::unordered_set<char>* m_passthrus;
const EncodeInfo* find_encode_to_utf8(const char* in) const;
const EncodeInfo* find_encode_to_game(const std::string& in, int off = 0) const;
const ReplaceInfo* find_replace_to_utf8(const std::string& in, int off = 0) const;
const ReplaceInfo* find_replace_to_game(const std::string& in, int off = 0) const;
std::string replace_to_utf8(std::string& str) const;
std::string replace_to_game(std::string& str) const;
std::string encode_utf8_to_game(std::string& str) const;
public:
GameTextFontBank(GameTextVersion version,
std::vector<EncodeInfo>* encode_info,
std::vector<ReplaceInfo>* replace_info,
std::unordered_set<char>* passthrus);
const std::vector<EncodeInfo>* encode_info() const { return m_encode_info; }
const std::vector<ReplaceInfo>* replace_info() const { return m_replace_info; }
const std::unordered_set<char>* passthrus() const { return m_passthrus; }
GameTextVersion version() const { return m_version; }
// TODO - methods would help make this code a lot better for different game versions
// hacking it for now
bool valid_char_range(const char in) const;
std::string convert_utf8_to_game(std::string str, bool escape = false) const;
std::string convert_game_to_utf8(const char* in) const;
};
extern std::map<GameTextVersion, GameTextFontBank*> g_font_banks;
const GameTextFontBank* get_font_bank(GameTextVersion version);
const GameTextFontBank* get_font_bank(const std::string& name);
bool font_bank_exists(GameTextVersion version);