Skip to content

Commit

Permalink
[ttf] added initial partial impl of tz::io::ttf. not close to functio…
Browse files Browse the repository at this point in the history
…nal, don't use
  • Loading branch information
harrand committed Sep 22, 2023
1 parent 66a6457 commit 2b82c74
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ add_library(topaz STATIC
# image
src/tz/io/image.cpp
src/tz/io/image.hpp
# ttf
src/tz/io/ttf.cpp
src/tz/io/ttf.hpp

# tz::lua
src/tz/lua/api.cpp
Expand Down
66 changes: 66 additions & 0 deletions src/tz/io/ttf.cpp
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);
}
}
}
40 changes: 40 additions & 0 deletions src/tz/io/ttf.hpp
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

0 comments on commit 2b82c74

Please sign in to comment.