TTF Loading Fixed
This commit is contained in:
51
src/dawntools/truetypetool/CMakeLists.txt
Normal file
51
src/dawntools/truetypetool/CMakeLists.txt
Normal file
@@ -0,0 +1,51 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
project(truetypetool VERSION 2.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}
|
||||
stb
|
||||
)
|
||||
|
||||
# Tool Function
|
||||
function(tool_truetype target in)
|
||||
set(DEPS "")
|
||||
if(DAWN_BUILD_TOOLS)
|
||||
set(DEPS truetypetool)
|
||||
endif()
|
||||
|
||||
add_custom_target(${target}
|
||||
COMMAND truetypetool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_ASSETS_BUILD_DIR}/${target}"
|
||||
COMMENT "Generating truetype font ${target} from ${in}"
|
||||
DEPENDS ${DEPS}
|
||||
)
|
||||
add_dependencies(${DAWN_TARGET_NAME} ${target})
|
||||
endfunction()
|
75
src/dawntools/truetypetool/TrueTypeTool.cpp
Normal file
75
src/dawntools/truetypetool/TrueTypeTool.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "TrueTypeTool.hpp"
|
||||
#ifndef STB_TRUETYPE_IMPLEMENTATION
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#include <stb_truetype.h>
|
||||
#endif
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> TrueTypeTool::getRequiredFlags() {
|
||||
return { "input", "output" };
|
||||
}
|
||||
|
||||
int32_t TrueTypeTool::start() {
|
||||
File fileIn(flags["input"]);
|
||||
char *ttfData = nullptr;
|
||||
if(!fileIn.readToBuffer(&ttfData)) {
|
||||
std::cout << "Failed to read file to buffer!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create bitmap data. This is a single channel color (alpha).
|
||||
size_t width = 1024;
|
||||
size_t height = 1024;
|
||||
float_t fontSize = 128.0f;
|
||||
size_t textureSize = width * height;
|
||||
stbi_uc *bitmapData = new stbi_uc[textureSize];
|
||||
stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS];
|
||||
|
||||
// Now parse the TTF itself.
|
||||
stbtt_BakeFontBitmap(
|
||||
(uint8_t*)ttfData, 0, (float)fontSize, bitmapData,
|
||||
width, height,
|
||||
TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS,
|
||||
characterData
|
||||
);
|
||||
|
||||
// Prepare output file for writing.
|
||||
File fileOut(flags["output"] + ".truetype");
|
||||
if(!fileOut.mkdirp()) {
|
||||
free(ttfData);
|
||||
delete bitmapData;
|
||||
std::cout << "Failed to create output directory!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if(!fileOut.open(FILE_MODE_WRITE)) {
|
||||
free(ttfData);
|
||||
delete bitmapData;
|
||||
std::cout << "Failed to open output file for writing!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write data
|
||||
fileOut.writeString(std::to_string(width) + "|" + std::to_string(height) + "|" + std::to_string(fontSize) + "|");
|
||||
uint8_t pixels[4];
|
||||
for(size_t i = 0; i < textureSize; i++) {
|
||||
pixels[0] = 255;
|
||||
pixels[1] = 255;
|
||||
pixels[2] = 255;
|
||||
pixels[3] = bitmapData[i];
|
||||
fileOut.writeRaw((char*)pixels, 4);
|
||||
}
|
||||
|
||||
// Write quads
|
||||
fileOut.writeRaw((char*)characterData, sizeof(stbtt_bakedchar) * TRUETYPE_NUM_CHARS);
|
||||
free(ttfData);
|
||||
delete bitmapData;
|
||||
fileOut.close();
|
||||
|
||||
return 0;
|
||||
}
|
22
src/dawntools/truetypetool/TrueTypeTool.hpp
Normal file
22
src/dawntools/truetypetool/TrueTypeTool.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// 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 "util/Image.hpp"
|
||||
|
||||
#define TRUETYPE_FIRST_CHAR 32
|
||||
#define TRUETYPE_NUM_CHARS 96
|
||||
|
||||
namespace Dawn {
|
||||
class TrueTypeTool : public DawnTool {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredFlags() override;
|
||||
|
||||
public:
|
||||
int32_t start();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user