Moved C++ tools out

This commit is contained in:
2023-10-31 21:15:03 -05:00
parent f8a715ec78
commit 343f75433e
98 changed files with 5 additions and 16 deletions

View File

@ -0,0 +1,67 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
project(truetypetool VERSION 3.0)
add_executable(truetypetool)
target_sources(truetypetool
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
TrueTypeTool.cpp
)
target_include_directories(truetypetool
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(truetypetool
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=TrueTypeTool
DAWN_TOOL_HEADER="TrueTypeTool.hpp"
)
# Libraries
target_link_libraries(truetypetool
PUBLIC
${DAWN_BUILD_HOST_LIBS}
)
# Tool Function
function(tool_truetype 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()
set(DEPS "")
if(DAWN_BUILD_TOOLS)
set(DEPS truetypetool)
endif()
add_custom_target(${target}
COMMAND truetypetool
--output="${DAWN_ASSETS_BUILD_DIR}/${target}.truetype"
--regular="${REGULAR}"
--bold="${BOLD}"
--italics="${ITALICS}"
--bold-italics="${BOLD_ITALICS}"
COMMENT "Generating truetype"
DEPENDS ${DEPS}
)
add_dependencies(dawnassets ${target})
endfunction()

View File

@ -0,0 +1,159 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeTool.hpp"
using namespace Dawn;
TrueTypeFile::TrueTypeFile(std::string path) : file(path) {
this->path = path;
// Remove extension
size_t pos = path.find_last_of(".");
std::string filename = path.substr(0, pos);
std::string pathLower = stringToLowercase(path);
style = 0;
if(pathLower.find("bold") != std::string::npos || filename.ends_with("bd") || filename.ends_with("bi")) {
style |= TRUE_TYPE_VARIANT_BOLD;
}
if(pathLower.find("italic") != std::string::npos || filename.ends_with("i") || filename.ends_with("bi")) {
style |= TRUE_TYPE_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!";
}
}
TrueTypeFile::~TrueTypeFile() {
}
std::vector<std::string> TrueTypeTool::getRequiredFlags() {
return { "output" };
}
std::map<std::string, std::string> TrueTypeTool::getOptionalFlags() {
return {
{ "regular", "" },
{ "italics", "" },
{ "bold", "" },
{ "bold-italics", "" }
};
}
int32_t TrueTypeTool::start() {
std::vector<TrueTypeFile*> 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 TrueTypeFile(path);
files.push_back(n);
} catch(const char *e) {
cleanupFiles();
return 1;
}
++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
cleanupFiles();
// Done
fileOut.close();
return 0;
}

View File

@ -0,0 +1,31 @@
// 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/TrueTypeShared.hpp"
#include "util/string.hpp"
namespace Dawn {
class TrueTypeFile {
public:
flag_t style;
std::string path;
File file;
TrueTypeFile(std::string path);
~TrueTypeFile();
};
class TrueTypeTool : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
std::map<std::string, std::string> getOptionalFlags() override;
public:
int32_t start();
};
}