Skip to content

Commit

Permalink
add KCHR decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzziqersoftware committed Oct 20, 2024
1 parent f370694 commit c5a0d63
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 190 deletions.
53 changes: 53 additions & 0 deletions src/ResourceFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4460,6 +4460,59 @@ string ResourceFile::decode_styl(shared_ptr<const Resource> res) const {
return ret;
}

ResourceFile::DecodedKeyCharMap ResourceFile::decode_KCHR(int16_t id, uint32_t type) const {
return this->decode_KCHR(this->get_resource(type, id));
}

ResourceFile::DecodedKeyCharMap ResourceFile::decode_KCHR(std::shared_ptr<const Resource> res) {
return ResourceFile::decode_KCHR(res->data.data(), res->data.size());
}

ResourceFile::DecodedKeyCharMap ResourceFile::decode_KCHR(const void* data, size_t size) {
StringReader r(data, size);
uint16_t version = r.get_u16b();
if (version != 0) {
throw runtime_error(string_printf("unknown KCHR version: %hu", version));
}

DecodedKeyCharMap ret;
for (size_t z = 0; z < ret.table_index_for_modifiers.size(); z++) {
ret.table_index_for_modifiers[z] = r.get_u8();
}

size_t num_tables = r.get_u16b();
for (size_t z = 0; z < ret.table_index_for_modifiers.size(); z++) {
if (ret.table_index_for_modifiers[z] >= num_tables) {
throw runtime_error(string_printf(
"table index %zX refers to out-of-bounds table %02hhX (there are only %zX tables)",
z, ret.table_index_for_modifiers[z], num_tables));
}
}
while (ret.tables.size() < num_tables) {
auto& table = ret.tables.emplace_back();
for (size_t z = 0; z < table.size(); z++) {
table[z] = r.get_u8();
}
}

size_t num_dead_keys = r.get_u16b();
while (ret.dead_keys.size() < num_dead_keys) {
auto& dead_key = ret.dead_keys.emplace_back();
dead_key.table_index = r.get_u8();
dead_key.virtual_key_code = r.get_u8();
size_t num_completions = r.get_u16b();
while (dead_key.completions.size() < num_completions) {
auto& completion = dead_key.completions.emplace_back();
completion.completion_char = r.get_u8();
completion.substitute_char = r.get_u8();
}
dead_key.no_match_completion.completion_char = r.get_u8();
dead_key.no_match_completion.substitute_char = r.get_u8();
}

return ret;
}

////////////////////////////////////////////////////////////////////////////////
// Font decoding

Expand Down
19 changes: 19 additions & 0 deletions src/ResourceFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,22 @@ public:
std::vector<Item> items;
};

struct DecodedKeyCharMap {
struct DeadKey {
struct Completion {
uint8_t completion_char;
uint8_t substitute_char;
};
uint8_t table_index;
uint8_t virtual_key_code;
std::vector<Completion> completions;
Completion no_match_completion;
};
std::array<uint8_t, 0x100> table_index_for_modifiers;
std::vector<std::array<uint8_t, 0x80>> tables;
std::vector<DeadKey> dead_keys;
};

struct TemplateEntry {
enum class Type {
VOID, // DVDR
Expand Down Expand Up @@ -790,6 +806,9 @@ public:
static std::string decode_TEXT(const void* data, size_t size);
std::string decode_styl(int16_t id, uint32_t type = RESOURCE_TYPE_styl) const;
std::string decode_styl(std::shared_ptr<const Resource> res) const;
DecodedKeyCharMap decode_KCHR(int16_t id, uint32_t type = RESOURCE_TYPE_KCHR) const;
static DecodedKeyCharMap decode_KCHR(std::shared_ptr<const Resource> res);
static DecodedKeyCharMap decode_KCHR(const void* data, size_t size);

// Font resources
DecodedFontResource decode_FONT(int16_t id, uint32_t type = RESOURCE_TYPE_FONT) const;
Expand Down
1 change: 1 addition & 0 deletions src/ResourceTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ constexpr uint32_t RESOURCE_TYPE_itlb = resource_type("itlb");
constexpr uint32_t RESOURCE_TYPE_itlc = resource_type("itlc");
constexpr uint32_t RESOURCE_TYPE_itlk = resource_type("itlk");
constexpr uint32_t RESOURCE_TYPE_KBDN = resource_type("KBDN");
constexpr uint32_t RESOURCE_TYPE_KCHR = resource_type("KCHR");
constexpr uint32_t RESOURCE_TYPE_kcs4 = resource_type("kcs4");
constexpr uint32_t RESOURCE_TYPE_kcs8 = resource_type("kcs8");
constexpr uint32_t RESOURCE_TYPE_kcsN = resource_type("kcs#");
Expand Down
Loading

0 comments on commit c5a0d63

Please sign in to comment.