Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/tools/fontcompiler/main.cpp

87 lines
2.3 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#include <cstdio>
#include <array>
#include <iostream>
2022-08-15 11:10:06 -04:00
#include <string_view>
2020-08-11 12:07:21 -04:00
#include <stb_truetype.h>
2022-08-15 11:10:06 -04:00
constexpr std::array sizes_to_pack = {36.0f, 24.0f};
2020-08-11 12:07:21 -04:00
constexpr int num_glyphs = 95;
constexpr int texture_width = 2048, texture_height = 1150;
stbtt_pack_context pack_context;
stbtt_packedchar packed_chars[sizes_to_pack.size()][num_glyphs];
stbtt_fontinfo font_info;
unsigned char* font_data = nullptr;
unsigned char font_bitmap[texture_width * texture_height];
void load_ttf(const std::string_view path) {
FILE* font_file = fopen(path.data(), "rb");
if (!font_file) {
std::cerr << "Unable to open font " << path << std::endl;
return;
}
fseek(font_file, 0, SEEK_END);
const unsigned long file_length = ftell(font_file);
fseek(font_file, 0, SEEK_SET);
font_data = new unsigned char[file_length];
fread(font_data, file_length, 1, font_file);
fclose(font_file);
stbtt_InitFont(&font_info, font_data, 0);
}
int main(int argc, char* argv[]) {
2022-08-15 11:10:06 -04:00
if (argc != 3) {
2020-08-11 12:07:21 -04:00
std::cout << "Usage: FontCompiler [input ttf] [output fp]" << std::endl;
return 0;
}
load_ttf(argv[1]);
stbtt_PackBegin(&pack_context, font_bitmap, texture_width, texture_height, 0, 1, nullptr);
stbtt_PackSetOversampling(&pack_context, 2, 2);
stbtt_pack_range ranges[sizes_to_pack.size()];
2022-08-15 11:10:06 -04:00
for (int i = 0; i < sizes_to_pack.size(); i++)
2020-08-11 12:07:21 -04:00
ranges[i] = {sizes_to_pack[i], 32, nullptr, 95, packed_chars[i], 0, 0};
stbtt_PackFontRanges(&pack_context, font_data, 0, ranges, sizes_to_pack.size());
stbtt_PackEnd(&pack_context);
FILE* file = fopen(argv[2], "wb");
2022-08-15 11:10:06 -04:00
if (!file)
2020-08-11 12:07:21 -04:00
return -1;
fwrite(&texture_width, sizeof(int), 1, file);
fwrite(&texture_height, sizeof(int), 1, file);
int ascent, descent, gap;
stbtt_GetFontVMetrics(&font_info, &ascent, &descent, &gap);
fwrite(&ascent, sizeof(int), 1, file);
fwrite(&descent, sizeof(int), 1, file);
fwrite(&gap, sizeof(int), 1, file);
2022-08-15 11:10:06 -04:00
for (auto& packed_char : packed_chars)
2021-10-14 08:51:58 -04:00
fwrite(&packed_char, sizeof(packed_char), 1, file);
2020-08-11 12:07:21 -04:00
2022-08-15 11:10:06 -04:00
for (auto& range : ranges) {
2021-10-14 08:51:58 -04:00
const float f = (ascent + descent) * stbtt_ScaleForPixelHeight(&font_info, range.font_size);
2020-08-11 12:07:21 -04:00
fwrite(&f, sizeof(float), 1, file);
}
fwrite(font_bitmap, texture_width * texture_height, 1, file);
fclose(file);
return 0;
}