2022-04-13 13:01:35 +02:00
|
|
|
#include "FontLoader.h"
|
2023-02-24 22:09:07 +01:00
|
|
|
#include "Asset/AssetRegistry.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Asset/FontAsset.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2023-11-05 10:36:01 +01:00
|
|
|
#include "Graphics/Resources.h"
|
2023-11-07 16:55:13 +01:00
|
|
|
#include "Graphics/Texture.h"
|
2023-01-29 18:58:59 +01:00
|
|
|
#include <ft2build.h>
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
#include FT_FREETYPE_H
|
2024-01-16 19:24:49 +01:00
|
|
|
#include <fstream>
|
2024-06-09 12:20:04 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2022-04-13 13:01:35 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
FontLoader::FontLoader(Gfx::PGraphics graphics) : graphics(graphics) {}
|
2022-04-13 13:01:35 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
FontLoader::~FontLoader() {}
|
2022-04-13 13:01:35 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void FontLoader::importAsset(FontImportArgs args) {
|
2023-02-01 22:13:04 +01:00
|
|
|
std::filesystem::path assetPath = args.filePath.filename();
|
2022-04-13 13:01:35 +02:00
|
|
|
assetPath.replace_extension("asset");
|
2023-11-05 10:36:01 +01:00
|
|
|
OFontAsset asset = new FontAsset(args.importPath, assetPath.stem().string());
|
2022-04-13 13:01:35 +02:00
|
|
|
asset->setStatus(Asset::Status::Loading);
|
2023-11-05 10:36:01 +01:00
|
|
|
// the registry takes ownership, but we need to edit the reference
|
|
|
|
|
PFontAsset ref = asset;
|
|
|
|
|
AssetRegistry::get().registerFont(std::move(asset));
|
|
|
|
|
import(args, ref);
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
// in case of the space character there is no bitmap
|
|
|
|
|
// so we create a single pixel empty texture
|
|
|
|
|
uint8 transparentPixel = 0;
|
2024-06-09 12:20:04 +02:00
|
|
|
void FontLoader::import(FontImportArgs args, PFontAsset asset) {
|
2023-01-29 18:58:59 +01:00
|
|
|
FT_Library ft;
|
|
|
|
|
FT_Error error = FT_Init_FreeType(&ft);
|
|
|
|
|
assert(!error);
|
|
|
|
|
FT_Face face;
|
2023-02-13 14:56:13 +01:00
|
|
|
error = FT_New_Face(ft, args.filePath.string().c_str(), 0, &face);
|
2023-01-29 18:58:59 +01:00
|
|
|
assert(!error);
|
|
|
|
|
FT_Set_Pixel_Sizes(face, 0, 48);
|
2023-11-07 16:55:13 +01:00
|
|
|
Array<Gfx::OTexture2D> usedTextures;
|
2024-06-09 12:20:04 +02:00
|
|
|
for (uint32 c = 0; c < 256; ++c) {
|
|
|
|
|
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
2023-01-29 18:58:59 +01:00
|
|
|
std::cout << "error loading " << (char)c << std::endl;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
FontAsset::Glyph& glyph = asset->glyphs[c];
|
|
|
|
|
glyph.size = IVector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
|
|
|
|
|
glyph.bearing = IVector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
|
|
|
|
|
glyph.advance = face->glyph->advance.x;
|
|
|
|
|
TextureCreateInfo imageData;
|
|
|
|
|
imageData.format = Gfx::SE_FORMAT_R8_UINT;
|
|
|
|
|
imageData.width = face->glyph->bitmap.width;
|
|
|
|
|
imageData.height = face->glyph->bitmap.rows;
|
2023-11-01 23:12:30 +01:00
|
|
|
imageData.sourceData.data = face->glyph->bitmap.buffer;
|
|
|
|
|
imageData.sourceData.size = imageData.width * imageData.height;
|
2024-06-09 12:20:04 +02:00
|
|
|
if (imageData.width == 0 || imageData.height == 0) {
|
2023-01-29 18:58:59 +01:00
|
|
|
glyph.size.x = 1;
|
|
|
|
|
glyph.size.y = 1;
|
|
|
|
|
glyph.bearing.x = 0;
|
|
|
|
|
glyph.bearing.y = 0;
|
|
|
|
|
imageData.width = 1;
|
|
|
|
|
imageData.height = 1;
|
2023-11-01 23:12:30 +01:00
|
|
|
imageData.sourceData.size = sizeof(uint8);
|
|
|
|
|
imageData.sourceData.data = &transparentPixel;
|
2023-01-29 18:58:59 +01:00
|
|
|
}
|
2023-11-07 16:55:13 +01:00
|
|
|
glyph.textureIndex = usedTextures.size();
|
|
|
|
|
usedTextures.add(graphics->createTexture2D(imageData));
|
2023-01-29 18:58:59 +01:00
|
|
|
}
|
|
|
|
|
FT_Done_Face(face);
|
|
|
|
|
FT_Done_FreeType(ft);
|
2023-11-07 16:55:13 +01:00
|
|
|
asset->setUsedTextures(std::move(usedTextures));
|
2023-07-31 21:43:20 +02:00
|
|
|
|
2024-07-19 10:42:59 +02:00
|
|
|
AssetRegistry::saveAsset(asset, FontAsset::IDENTIFIER, asset->getFolderPath(), asset->getName());
|
2023-07-31 21:43:20 +02:00
|
|
|
|
2023-01-29 18:58:59 +01:00
|
|
|
asset->setStatus(Asset::Status::Ready);
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|