mirror of
https://github.com/open-goal/jak-project
synced 2026-05-23 06:54:31 -04:00
006d24b29a
Resolves #3075 TODO before merge: - [x] Properly draw non-korean strings while in korean mode (language selection) - [x] Check jak 3 - [x] Translation scaffolding (allow korean characters, add to Crowdin, fix japanese locale, etc) - [x] Check translation of text lines - [x] Check translation of subtitle lines - [x] Cleanup PR / some performance optimization (it's take a bit too long to build the text and it shouldn't since the information is in a giant lookup table) - [x] Wait until release is cut I confirmed the font textures are identical between Jak 2 and Jak 3, so thank god for that. Some examples of converting the korean encoding to utf-8. These show off all scenarios, pure korean / korean with ascii and japanese / korean with replacements (flags): <img width="316" height="611" alt="Screenshot 2025-07-26 191511" src="https://github.com/user-attachments/assets/614383ba-8049-4bf4-937e-24ad3e605d41" /> <img width="254" height="220" alt="Screenshot 2025-07-26 191529" src="https://github.com/user-attachments/assets/1f6e5a6c-8527-4f98-a988-925ec66e437d" /> And it working in game. `Input Options` is a custom not-yet-translated string. It now shows up properly instead of a disgusting block of glyphs, and all the original strings are hopefully the same semantically!: <img width="550" height="493" alt="Screenshot 2025-07-26 202838" src="https://github.com/user-attachments/assets/9ebdf6c0-f5a3-4a30-84a1-e5840809a1a2" /> Quite the challenge. The crux of the problem is -- Naughty Dog came up with their own encoding for representing korean syllable blocks, and that source information is lost so it has to be reverse engineered. Instead of trying to figure out their encoding from the text -- I went at it from the angle of just "how do i draw every single korean character using their glyph set". One might think this is way too time consuming but it's important to remember: - Korean letters are designed to be composable from a relatively small number of glyphs (more on this later) - Someone at naughty dog did basically this exact process - There is no other way! While there are loose patterns, there isn't an overarching rhyme or reason, they just picked the right glyph for the writing context (more on this later). And there are even situations where there IS NO good looking glyph, or the one ND chose looks awful and unreadable (we could technically fix this by adjusting the positioning of the glyphs but....no more)! Information on their encoding that gets passed to `convert-korean-text`: - It's a raw stream of bytes - It can contain normal font letters - Every syllable block begins with: `0x04 <num_glyphs> <...the glyph bytes...>` - DO NOT confuse `num_glyphs` with num jamo, because some glyphs can have multiple jamo! - Every section of normal text starts with `0x03`. For example a space would be `0x03 0x20` - There are a very select few number of jamo glyphs on a secondary texture page, these glyph bytes are preceeded with a `0x05`. These jamo are a variant of some of the final vowels, moving them as low down as possible. Crash course on korean writing: - Nice resource as this is basically what we are doing - https://glyphsapp.com/learn/creating-a-hangeul-font - Korean syllable blocks have either 2 or 3 jamo. Jamo are basically letters and are the individual pieces that make up the syllable blocks. - The jamo are split up into "initial", "medial" and "final" categories. Within the "medial" category there are obvious visual variants: - Horizontal - Vertical - Combination (horizontal + a vertical) - These jamo are laid out in 6 main pre-defined "orientations": - initial + vertical medial - initial + horizontal medial - initial + combination - initial + vertical medial + final - initial + horizontal medial + final - initial + combination + final - Sometimes, for stylistic reasons, jamo will be written in different ways (ie. if there is nothing below a vertical vowel will be extended). - Annoying, and ND's glyph set supports this stylistic choice! - There are some combination of jamo that are never used, and some that are only used for a single word in the entire language! With all that in mind, my basic process was: - Scan the game's entire corpus of korean text, that includes subtitles. It's very easy to look at the font texture's glyphs and assign them to their respective jamo - This let me construct a mapping and see which glyphs were used under which context - I then shoved this information into a 2-D matrix in excel, and created an in-game tool to check every single jamo permutation to fill in the gaps / change them if naughty dogs was bad. Most of the time, ND's encoding was fine. - https://docs.google.com/spreadsheets/d/e/2PACX-1vTtyMeb5-mL5rXseS9YllVj32BGCISOGZFic6nkRV5Er5aLZ9CLq1Hj_rTY7pRCn-wrQDH1rvTqUHwB/pubhtml?gid=886895534&single=true anything in red is an addition / modification on my part. - This was the most lengthy part but not as long as you may think, you can do a lot of pruning. For example if you are checking a 3-jamo variant (the ones with the most permutations) and you've verified that the medial jamo is as far up vertically as it can be, and you are using the lowest final jamo that are available -- there is nothing to check or improve -- for better or worse! So those end up being the permutations between the initial and medial instead of a three-way permutation nightmare. - Also, while it is a 2d matrix, there's a lot of pruning even within that. For example, for the first 3 orientations, you dont have to care about final vowels at all. - At the end, I'm left with a lookup table that I can use the encode the best looking korean syllable blocks possible given the context of the jamo combination.
359 lines
13 KiB
C++
359 lines
13 KiB
C++
#include "text_ser.h"
|
|
|
|
#include "common/goos/ParseHelpers.h"
|
|
#include "common/goos/Reader.h"
|
|
#include "common/util/font/font_utils_korean.h"
|
|
|
|
int64_t get_int(const goos::Object& obj) {
|
|
if (obj.is_int()) {
|
|
return obj.integer_obj.value;
|
|
}
|
|
throw std::runtime_error(obj.print() + " was supposed to be an integer, but isn't");
|
|
}
|
|
|
|
const goos::Object& car(const goos::Object& x) {
|
|
if (!x.is_pair()) {
|
|
throw std::runtime_error("car: invalid pair");
|
|
}
|
|
|
|
return x.as_pair()->car;
|
|
}
|
|
|
|
const goos::Object& cdr(const goos::Object& x) {
|
|
if (!x.is_pair()) {
|
|
throw std::runtime_error("cdr: invalid pair");
|
|
}
|
|
|
|
return x.as_pair()->cdr;
|
|
}
|
|
|
|
std::string get_string(const goos::Object& x) {
|
|
if (x.is_string()) {
|
|
return x.as_string()->data;
|
|
}
|
|
throw std::runtime_error(x.print() + " was supposed to be a string, but isn't");
|
|
}
|
|
|
|
/*!
|
|
* Parse a game text file (GOAL format).
|
|
* Information is added to the game text database.
|
|
*
|
|
* The file should begin with (language-id x y z...) with the given language IDs.
|
|
* Each entry should be (id "line for 1st language" "line for 2nd language" ...)
|
|
* This adds the text line to each of the specified languages.
|
|
*/
|
|
void parse_text_goal(const goos::Object& data,
|
|
GameTextDB& db,
|
|
const GameTextDefinitionFile& /*file_info*/) {
|
|
GameTextFontBank* font = nullptr;
|
|
std::vector<std::shared_ptr<GameTextBank>> banks;
|
|
std::string possible_group_name;
|
|
|
|
for_each_in_list(data.as_pair()->cdr, [&](const goos::Object& obj) {
|
|
if (obj.is_pair()) {
|
|
auto& head = car(obj);
|
|
if (head.is_symbol("language-id")) {
|
|
if (banks.size() != 0) {
|
|
throw std::runtime_error("Languages have been set multiple times.");
|
|
}
|
|
|
|
if (cdr(obj).is_empty_list()) {
|
|
throw std::runtime_error("At least one language must be set.");
|
|
}
|
|
|
|
if (possible_group_name.empty()) {
|
|
throw std::runtime_error("Text group must be set before languages.");
|
|
}
|
|
|
|
for_each_in_list(cdr(obj), [&](const goos::Object& obj) {
|
|
auto lang = get_int(obj);
|
|
if (!db.bank_exists(possible_group_name, lang)) {
|
|
// database has no lang in this group yet
|
|
banks.push_back(db.add_bank(possible_group_name, std::make_shared<GameTextBank>(lang)));
|
|
} else {
|
|
banks.push_back(db.bank_by_id(possible_group_name, lang));
|
|
}
|
|
});
|
|
} else if (head.is_symbol("group-name")) {
|
|
if (!possible_group_name.empty()) {
|
|
throw std::runtime_error("group-name has been set multiple times.");
|
|
}
|
|
|
|
possible_group_name = get_string(car(cdr(obj)));
|
|
|
|
if (possible_group_name.empty()) {
|
|
throw std::runtime_error("invalid group-name.");
|
|
}
|
|
|
|
if (!cdr(cdr(obj)).is_empty_list()) {
|
|
throw std::runtime_error("group-name has too many arguments");
|
|
}
|
|
} else if (head.is_symbol("credits")) {
|
|
// parse a "credits" object. it's a list of lines where the ID automatically increments, and
|
|
// empty lines are skipped
|
|
if (banks.size() == 0) {
|
|
throw std::runtime_error("At least one language must be set before defining entries.");
|
|
}
|
|
|
|
if (cdr(obj).is_empty_list() || cdr(cdr(obj)).is_empty_list() ||
|
|
!car(cdr(obj)).is_symbol(":begin") || !car(cdr(cdr(obj))).is_int()) {
|
|
throw std::runtime_error("Invalid credits begin param");
|
|
}
|
|
|
|
const auto& it = cdr(cdr(obj));
|
|
int begin_id = car(it).as_int();
|
|
int id = begin_id - 1;
|
|
for_each_in_list(cdr(it), [&](const goos::Object& entry) {
|
|
++id;
|
|
if (entry.is_string()) {
|
|
if (entry.as_string()->data.empty()) {
|
|
// empty string! just advance
|
|
return;
|
|
}
|
|
|
|
auto line = font->convert_utf8_to_game(entry.as_string()->data);
|
|
// add to all langs
|
|
for (auto& bank : banks) {
|
|
bank->set_line(id, line);
|
|
}
|
|
} else if (entry.is_pair()) {
|
|
int b_i = 0;
|
|
for_each_in_list(entry, [&](const goos::Object& entry) {
|
|
if (entry.is_string()) {
|
|
if (b_i >= int(banks.size())) {
|
|
throw std::runtime_error(fmt::format("Too many strings in text id #x{:x}", id));
|
|
}
|
|
if (font->is_language_id_korean(b_i)) {
|
|
// korean changes differently!
|
|
auto line = font->convert_utf8_to_game_korean(entry.as_string()->data);
|
|
banks[b_i]->set_line(id, line);
|
|
} else {
|
|
auto line = font->convert_utf8_to_game(entry.as_string()->data);
|
|
banks[b_i]->set_line(id, line);
|
|
}
|
|
b_i++;
|
|
} else {
|
|
throw std::runtime_error(fmt::format("Non-string value in text id #x{:x}", id));
|
|
}
|
|
});
|
|
if (b_i != int(banks.size())) {
|
|
throw std::runtime_error(
|
|
fmt::format("Not enough strings specified in text id #x{:x}", id));
|
|
}
|
|
} else {
|
|
throw std::runtime_error(fmt::format("Non-string value in text id #x{:x}", id));
|
|
}
|
|
});
|
|
} else if (head.is_symbol("text-version")) {
|
|
if (font) {
|
|
throw std::runtime_error("text version is already set");
|
|
}
|
|
|
|
const auto& ver_name = car(cdr(obj));
|
|
if (!ver_name.is_symbol()) {
|
|
throw std::runtime_error("invalid text version entry");
|
|
}
|
|
|
|
font = get_font_bank(ver_name.as_symbol().name_ptr);
|
|
}
|
|
|
|
else if (head.is_int()) {
|
|
if (!font) {
|
|
throw std::runtime_error("Text version must be set before defining entries.");
|
|
}
|
|
if (banks.size() == 0) {
|
|
throw std::runtime_error("At least one language must be set before defining entries.");
|
|
}
|
|
int i = 0;
|
|
int id = head.as_int();
|
|
for_each_in_list(cdr(obj), [&](const goos::Object& entry) {
|
|
if (entry.is_string()) {
|
|
if (i >= int(banks.size())) {
|
|
throw std::runtime_error(fmt::format("Too many strings in text id #x{:x}", id));
|
|
}
|
|
|
|
if (font->is_language_id_korean(i)) {
|
|
// handle korean differently!
|
|
auto line = font->convert_utf8_to_game_korean(entry.as_string()->data);
|
|
banks[i]->set_line(id, line);
|
|
} else {
|
|
auto line = font->convert_utf8_to_game(entry.as_string()->data);
|
|
banks[i]->set_line(id, line);
|
|
}
|
|
i++;
|
|
} else {
|
|
throw std::runtime_error(fmt::format("Non-string value in text id #x{:x}", id));
|
|
}
|
|
});
|
|
if (i != int(banks.size())) {
|
|
throw std::runtime_error(
|
|
fmt::format("Not enough strings specified in text id #x{:x}", id));
|
|
}
|
|
} else {
|
|
throw std::runtime_error("Invalid game text file entry: " + head.print());
|
|
}
|
|
} else {
|
|
throw std::runtime_error("Invalid game text file");
|
|
}
|
|
});
|
|
if (banks.size() == 0) {
|
|
throw std::runtime_error("At least one language must be set.");
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* Parse a game text file (JSON format).
|
|
* Information is added to the game text database.
|
|
*
|
|
* Each single text entry should follow this format:
|
|
* <text_id>: <text_value>
|
|
* For example, for (display #x1043) you would add this entry:
|
|
* "1043": "DISPLAY"
|
|
*
|
|
* You can also set multiple sequential lines starting from an initial text_id:
|
|
* <text_id>: [<text_value_1>, <text_value_2>, etc]
|
|
* For example, Jak 1 credits start at #x0b00, so this would override the first 3 lines:
|
|
* "0b00": [
|
|
* "JAK AND DAXTER: CREDITS LINE 1",
|
|
* "CREDITS LINE 2",
|
|
* "CREDITS LINE 3"
|
|
* ]
|
|
*/
|
|
void parse_text_json(const nlohmann::json& json,
|
|
GameTextDB& db,
|
|
const GameTextDefinitionFile& file_info) {
|
|
// Verify we have all data that we need
|
|
if (!file_info.group_name.has_value()) {
|
|
throw std::runtime_error(
|
|
fmt::format("Can't parse {}, did not provide group_name", file_info.file_path));
|
|
}
|
|
if (file_info.language_id == -1) {
|
|
throw std::runtime_error(
|
|
fmt::format("Can't parse {}, did not provide language_id", file_info.file_path));
|
|
}
|
|
if (file_info.text_version.empty()) {
|
|
throw std::runtime_error(
|
|
fmt::format("Can't parse {}, did not provide text_version", file_info.file_path));
|
|
}
|
|
// Init Settings
|
|
std::shared_ptr<GameTextBank> bank;
|
|
if (!db.bank_exists(file_info.group_name.value(), file_info.language_id)) {
|
|
// database has no lang in this group yet
|
|
bank = db.add_bank(file_info.group_name.value(),
|
|
std::make_shared<GameTextBank>(file_info.language_id));
|
|
} else {
|
|
bank = db.bank_by_id(file_info.group_name.value(), file_info.language_id);
|
|
}
|
|
GameTextFontBank* font = get_font_bank(file_info.text_version);
|
|
// Parse the file
|
|
for (const auto& [text_id, text_value] : json.items()) {
|
|
auto line_id = std::stoi(text_id, nullptr, 16);
|
|
if (text_value.is_string()) {
|
|
// single line replacement
|
|
if (font->is_language_id_korean(file_info.language_id)) {
|
|
auto line = font->convert_utf8_to_game_korean(text_value);
|
|
bank->set_line(line_id, line);
|
|
} else {
|
|
auto line = font->convert_utf8_to_game(text_value);
|
|
bank->set_line(line_id, line);
|
|
}
|
|
|
|
} else if (text_value.is_array()) {
|
|
// multi-line replacement starting from line_id
|
|
// (e.g. for Jak 1 credits, start from x0b00)
|
|
for (const auto& [idx, raw_line] : text_value.items()) {
|
|
if (!raw_line.is_string()) {
|
|
throw std::runtime_error(fmt::format(
|
|
"Non string provided for line {} / text id #x{} of _credits", idx, line_id));
|
|
}
|
|
|
|
if (font->is_language_id_korean(file_info.language_id)) {
|
|
auto line = font->convert_utf8_to_game_korean(raw_line);
|
|
bank->set_line(line_id++, line); // increment line_id
|
|
} else {
|
|
auto line = font->convert_utf8_to_game(raw_line);
|
|
bank->set_line(line_id++, line); // increment line_id
|
|
}
|
|
}
|
|
} else {
|
|
// Unexpected value type
|
|
throw std::runtime_error(
|
|
fmt::format("Must provide string or array for text id #x{}", text_id));
|
|
}
|
|
}
|
|
}
|
|
|
|
GameTextVersion parse_text_only_version(const std::string& filename) {
|
|
goos::Reader reader;
|
|
return parse_text_only_version(reader.read_from_file({filename}));
|
|
}
|
|
|
|
GameTextVersion parse_text_only_version(const goos::Object& data) {
|
|
const GameTextFontBank* font = nullptr;
|
|
for_each_in_list(data.as_pair()->cdr, [&](const goos::Object& obj) {
|
|
if (obj.is_pair()) {
|
|
auto& head = car(obj);
|
|
if (head.is_symbol("text-version")) {
|
|
if (font) {
|
|
throw std::runtime_error("text version is already set");
|
|
}
|
|
|
|
const auto& ver_name = car(cdr(obj));
|
|
if (!ver_name.is_symbol()) {
|
|
throw std::runtime_error("invalid text version entry");
|
|
}
|
|
|
|
font = get_font_bank(ver_name.as_symbol().name_ptr);
|
|
}
|
|
}
|
|
});
|
|
if (!font) {
|
|
throw std::runtime_error("text version not found");
|
|
}
|
|
return font->version();
|
|
}
|
|
|
|
void open_text_project(const std::string& kind,
|
|
const std::string& filename,
|
|
std::vector<GameTextDefinitionFile>& text_files) {
|
|
goos::Reader reader;
|
|
auto& proj = reader.read_from_file({filename}).as_pair()->cdr.as_pair()->car;
|
|
if (!proj.is_pair() || !proj.as_pair()->car.is_symbol() ||
|
|
proj.as_pair()->car.as_symbol() != kind) {
|
|
throw std::runtime_error(fmt::format("invalid {} project", kind));
|
|
}
|
|
|
|
goos::for_each_in_list(proj.as_pair()->cdr, [&](const goos::Object& o) {
|
|
if (o.is_pair() && o.as_pair()->cdr.is_pair()) {
|
|
auto args = o.as_pair();
|
|
auto& action = args->car.as_symbol();
|
|
args = args->cdr.as_pair();
|
|
|
|
if (action == "file") {
|
|
auto& file_path = args->car.as_string()->data;
|
|
auto new_file = GameTextDefinitionFile();
|
|
new_file.format = GameTextDefinitionFile::Format::GOAL;
|
|
new_file.file_path = file_path;
|
|
text_files.push_back(new_file);
|
|
} else if (action == "file-json") {
|
|
auto& language_id = args->car.as_int();
|
|
args = args->cdr.as_pair();
|
|
auto& text_version = args->car.as_symbol();
|
|
args = args->cdr.as_pair();
|
|
std::optional<std::string> group_name = std::nullopt;
|
|
group_name = args->car.as_string()->data;
|
|
args = args->cdr.as_pair()->car.as_pair();
|
|
goos::for_each_in_list(args->cdr.as_pair()->car, [&](const goos::Object& o) {
|
|
text_files.push_back({GameTextDefinitionFile::Format::JSON, o.as_string()->data,
|
|
(int)language_id, text_version.name_ptr, group_name});
|
|
});
|
|
} else {
|
|
throw std::runtime_error(
|
|
fmt::format("unknown action {} in {} project", action.name_ptr, kind));
|
|
}
|
|
} else {
|
|
throw std::runtime_error(fmt::format("invalid entry in {} project", kind));
|
|
}
|
|
});
|
|
}
|