-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ttf] added initial partial impl of tz::io::ttf. not close to functio…
…nal, don't use
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "tz/io/ttf.hpp" | ||
#include "tz/core/debug.hpp" | ||
#include "tz/core/profile.hpp" | ||
#include <fstream> | ||
#include <cstdio> | ||
|
||
namespace tz::io | ||
{ | ||
ttf ttf::from_memory(std::string_view sv) | ||
{ | ||
TZ_PROFZONE("ttf - from memory", 0xFFFF2222); | ||
return {sv}; | ||
} | ||
|
||
ttf ttf::from_file(const char* path) | ||
{ | ||
TZ_PROFZONE("ttf - from file", 0xFFFF2222); | ||
std::ifstream file(path, std::ios::binary); | ||
tz::assert(file.good(), "Could not load ttf from file because the path was wrong, or something else went wrong."); | ||
std::string buffer(std::istreambuf_iterator<char>(file), {}); | ||
return ttf::from_memory(buffer); | ||
} | ||
|
||
ttf::ttf(std::string_view ttf_data) | ||
{ | ||
this->parse_header(ttf_data); | ||
this->parse_table_info(ttf_data); | ||
} | ||
|
||
void ttf::parse_header(std::string_view& str) | ||
{ | ||
const char* ptr = str.data(); | ||
this->header.scalar_type = *reinterpret_cast<const std::uint32_t*>(ptr); | ||
ptr += sizeof(std::uint32_t); | ||
this->header.num_tables = *reinterpret_cast<const std::uint16_t*>(ptr); | ||
ptr += sizeof(std::uint16_t); | ||
this->header.search_range = *reinterpret_cast<const std::uint16_t*>(ptr); | ||
ptr += sizeof(std::uint16_t); | ||
this->header.entry_selector = *reinterpret_cast<const std::uint16_t*>(ptr); | ||
ptr += sizeof(std::uint16_t); | ||
this->header.range_shift = *reinterpret_cast<const std::uint16_t*>(ptr); | ||
ptr += sizeof(std::uint16_t); | ||
|
||
auto byte_diff = std::distance(str.data(), ptr); | ||
tz::assert(std::cmp_greater(str.size(), byte_diff)); | ||
str.remove_prefix(byte_diff); | ||
} | ||
|
||
void ttf::parse_table_info(std::string_view str) | ||
{ | ||
const char* ptr = str.data(); | ||
tz::assert(this->tables.empty()); | ||
for(std::size_t i = 0; i < this->header.num_tables; i++) | ||
{ | ||
ttf_table& tbl = this->tables.emplace_back(); | ||
std::memcpy(tbl.tag, ptr, 4); | ||
ptr += 4; | ||
tbl.checksum = *reinterpret_cast<const std::uint32_t*>(ptr); | ||
ptr += sizeof(std::uint32_t); | ||
tbl.offset = *reinterpret_cast<const std::uint32_t*>(ptr); | ||
ptr += sizeof(std::uint32_t); | ||
tbl.length = *reinterpret_cast<const std::uint32_t*>(ptr); | ||
ptr += sizeof(std::uint32_t); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef TOPAZ_IO_TTF_HPP | ||
#define TOPAZ_IO_TTF_HPP | ||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace tz::io | ||
{ | ||
struct ttf_header | ||
{ | ||
std::uint32_t scalar_type = 0u; | ||
std::uint16_t num_tables = 0u; | ||
std::uint16_t search_range = 0u; | ||
std::uint16_t entry_selector = 0u; | ||
std::uint16_t range_shift = 0u; | ||
}; | ||
|
||
struct ttf_table | ||
{ | ||
char tag[4]; | ||
std::uint32_t checksum = 0u; | ||
std::uint32_t offset = 0u; | ||
std::uint32_t length = 0u; | ||
}; | ||
|
||
class ttf | ||
{ | ||
public: | ||
ttf() = default; | ||
static ttf from_memory(std::string_view sv); | ||
static ttf from_file(const char* path); | ||
ttf(std::string_view ttf_data); | ||
private: | ||
void parse_header(std::string_view& str); | ||
void parse_table_info(std::string_view str); | ||
ttf_header header = {}; | ||
std::vector<ttf_table> tables = {}; | ||
}; | ||
} | ||
|
||
#endif //TOPAZ_IO_TTF_HPP |