Font loading progress

This commit is contained in:
2023-06-09 09:04:45 -07:00
parent d70ae88359
commit 8b3ecc88a6
18 changed files with 557 additions and 54 deletions

View File

@@ -26,4 +26,5 @@ add_subdirectory(prefabtool)
add_subdirectory(scenetool)
add_subdirectory(texturetool)
add_subdirectory(truetypetool)
add_subdirectory(vnscenetool)
add_subdirectory(vnscenetool)
add_subdirectory(newtruetypetool)

View File

@@ -0,0 +1,70 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
project(newtruetypetool VERSION 3.0)
add_executable(newtruetypetool)
target_sources(newtruetypetool
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
NewTrueTypeTool.cpp
)
target_include_directories(newtruetypetool
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(newtruetypetool
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=NewTrueTypeTool
DAWN_TOOL_HEADER="NewTrueTypeTool.hpp"
)
# Libraries
target_link_libraries(newtruetypetool
PUBLIC
${DAWN_BUILD_HOST_LIBS}
)
# Tool Function
function(tool_newtruetype target)
# Defaults
set(FILE "" )
# Parse Args
foreach(_PAIR IN LISTS ARGN)
if (_PAIR MATCHES "^([^:]+)=(.*)$")
set(${CMAKE_MATCH_1} ${CMAKE_MATCH_2})
else()
message(FATAL_ERROR "Invalid pair: ${_PAIR}")
endif()
endforeach()
# Check for missing args
set(DEPS "")
if(DAWN_BUILD_TOOLS)
set(DEPS truetypetool)
endif()
add_custom_target(${target}
COMMAND newtruetypetool
--output="${DAWN_ASSETS_BUILD_DIR}/${target}.newtruetype"
--regular="${REGULAR}"
--bold="${BOLD}"
--italics="${ITALICS}"
--bold-italics="${BOLD_ITALICS}"
COMMENT "Generating newtruetype"
DEPENDS ${DEPS}
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()

View File

@@ -0,0 +1,160 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "NewTrueTypeTool.hpp"
using namespace Dawn;
NewTrueTypeFile::NewTrueTypeFile(std::string path) : file(path) {
this->path = path;
style = 0;
if(path.find("bold") != std::string::npos) {
style |= NEW_TRUETYPE_VARIANT_BOLD;
}
if(path.find("italics") != std::string::npos) {
style |= NEW_TRUETYPE_VARIANT_ITALICS;
}
if(!file.exists()) {
std::cout << "File " << path << " does not exist!" << std::endl;
throw "File not found";
}
if(!file.open(FILE_MODE_READ)) {
std::cout << "Failed to open file " << path << " for reading!" << std::endl;
throw "Unable to open file for reading!";
}
}
NewTrueTypeFile::~NewTrueTypeFile() {
}
std::vector<std::string> NewTrueTypeTool::getRequiredFlags() {
return { "output" };
}
std::map<std::string, std::string> NewTrueTypeTool::getOptionalFlags() {
return {
{ "regular", "" },
{ "italics", "" },
{ "bold", "" },
{ "bold-italics", "" }
};
}
int32_t NewTrueTypeTool::start() {
std::vector<NewTrueTypeFile*> files;
std::vector<std::string> flags = { "regular", "italics", "bold", "bold-italics" };
auto cleanupFiles = [&]() {
auto itFile = files.begin();
while(itFile != files.end()) {
auto file = *itFile;
delete file;
++itFile;
}
};
// For each flag
auto itFlag = flags.begin();
while(itFlag != flags.end()) {
std::string flag = *itFlag;
std::string path = this->flags[flag];
if(path.empty()) {
++itFlag;
continue;
}
try {
auto n = new NewTrueTypeFile(path);
files.push_back(n);
} catch(const char *e) {
cleanupFiles();
return 1;
}
std::cout << "Push back" << std::endl;
++itFlag;
}
if(files.size() == 0) {
std::cout << "No valid TTF files provided!" << std::endl;
cleanupFiles();
return 1;
}
// Create the output file
File fileOut = File(this->flags["output"]);
if(!fileOut.mkdirp()) {
std::cout << "Failed to create output directory!" << std::endl;
cleanupFiles();
return 1;
}
if(!fileOut.open(FILE_MODE_WRITE)) {
std::cout << "Failed to open output file for writing!" << std::endl;
cleanupFiles();
return 1;
}
// Prepare to write data
std::string header = "DE_TTF|3.00|";
// Write file count
header += std::to_string(files.size()) + "|";
// For each file
auto itFile = files.begin();
while(itFile != files.end()) {
auto file = *itFile;
// Style
header += std::to_string(file->style);
header += ":";
// File length
header += std::to_string(file->file.length);
header += "|";
++itFile;
}
if(!fileOut.writeRaw((char *)header.c_str(), header.length())) {
std::cout << "Failed to write TTF Header to " << fileOut.filename << std::endl;
cleanupFiles();
return 1;
}
// Now write the data for each file
itFile = files.begin();
while(itFile != files.end()) {
auto file = *itFile;
// Write the file data
file->file.setPosition(0);
if(!fileOut.copyRaw(&file->file, file->file.length)) {
std::cout << "Failed copy output data of " << file->file.filename << std::endl;
cleanupFiles();
return 1;
}
// Write vertical bar
char sep[1];
sep[0] = '|';
fileOut.writeRaw(sep, 1);
++itFile;
}
// Cleanup
itFile = files.begin();
while(itFile != files.end()) {
auto file = *itFile;
delete file;
++itFile;
}
// Done
fileOut.close();
return 0;
}

View File

@@ -0,0 +1,30 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/DawnTool.hpp"
#include "util/File.hpp"
#include "display/font/truetype/NewTrueTypeShared.hpp"
namespace Dawn {
class NewTrueTypeFile {
public:
flag_t style;
std::string path;
File file;
NewTrueTypeFile(std::string path);
~NewTrueTypeFile();
};
class NewTrueTypeTool : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
std::map<std::string, std::string> getOptionalFlags() override;
public:
int32_t start();
};
}

View File

@@ -178,6 +178,16 @@ size_t File::readToBuffer(char **buffer) {
return l;
}
size_t File::readRaw(char *buffer, size_t max) {
assertNotNull(buffer);
assertTrue(max > 0);
if(!this->isOpen()) {
if(!this->open(FILE_MODE_READ)) return 0;
}
assertTrue(this->mode == FILE_MODE_READ);
return fread(buffer, sizeof(char), max, this->file);
}
bool_t File::writeString(std::string in) {
if(!this->isOpen() && !this->open(FILE_MODE_WRITE)) return false;
assertTrue(this->mode == FILE_MODE_WRITE);
@@ -187,10 +197,30 @@ bool_t File::writeString(std::string in) {
bool_t File::writeRaw(char *data, size_t len) {
if(!this->isOpen() && !this->open(FILE_MODE_WRITE)) return false;
assertTrue(this->mode == FILE_MODE_WRITE);
assertTrue(len > 0);
this->length = fwrite(data, sizeof(char_t), len, this->file);
return true;
}
bool_t File::copyRaw(File *otherFile, size_t length) {
assertTrue(length > 0);
assertTrue(otherFile->isOpen());
char buffer[FILE_BUFFER_SIZE];
size_t amountLeftToRead = length;
size_t read = 0;
while(amountLeftToRead > 0) {
auto iRead = otherFile->readRaw(buffer, mathMin<size_t>(FILE_BUFFER_SIZE, amountLeftToRead));
if(iRead == 0) return false;
this->writeRaw(buffer, iRead);
amountLeftToRead -= iRead;
read += iRead;
}
assertTrue(read == length);
return true;
}
void File::setPosition(size_t n) {
fseek(this->file, 0, SEEK_SET);
fseek(this->file, n, SEEK_CUR);

View File

@@ -118,6 +118,15 @@ namespace Dawn {
*/
size_t readToBuffer(char **buffer);
/**
* Reads the contents of this file into a given buffer.
*
* @param buffer Pointer to buffer to read to.
* @param max Max length of the buffer.
* @return Amount of bytes read.
*/
size_t readRaw(char *buffer, size_t max);
/**
* Writes the entire contents of a string to a file.
*
@@ -130,9 +139,19 @@ namespace Dawn {
* Write raw bytes to the file.
*
* @param data Data to write.
* @param size Size of the data to write.
* @return True if written successfully, otherwise false.
*/
bool_t writeRaw(char *data, size_t );
bool_t writeRaw(char *data, size_t size);
/**
* Write raw bytes to the file from another file.
*
* @param otherFile Other file to read from.
* @param length Length of the data to read.
* @return True if written successfully, otherwise false.
*/
bool_t copyRaw(File *otherFile, size_t length);
/**
* Set the position of the cursor of the file reader.