#include #include FT_FREETYPE_H #include "Graphics.h" #include namespace { FT_Library library; FT_Error error; FT_Face face; } namespace ADVect::Graphics { void Init() { error = FT_Init_FreeType(&library); if (error) { std::cerr << "FreeType init error: " << error << std::endl; } error = FT_New_Face(library, "SourceHanSans-Regular.ttc", 0, &face); if (error == FT_Err_Unknown_File_Format) { std::cerr << "FreeType Unknown_File_Format: " << error << std::endl; } else if (error) { std::cerr << "FreeType font loading error: " << error << std::endl; } error = FT_Set_Char_Size(face, 0, 20 * 64, 72, 72); if (error == FT_Err_Unknown_File_Format) { std::cerr << "FreeType Set_Char_Size error: " << error << std::endl; } } void Destroy() { FT_Done_Face(face); FT_Done_FreeType(library); } void RenderString(const NVL::String& s, u32 pos_x, u32 pos_y, const SDL_Color& attr) { FT_GlyphSlot slot = face->glyph; SDL_Rect pos = { pos_x, pos_y, 0, 0 }; SDL_Color colors[256]; for (u16 i = 0; i < 256; i++) { colors[i].r = f32(i) / 255.f * (f32)attr.r; colors[i].g = f32(i) / 255.f * (f32)attr.g; colors[i].b = f32(i) / 255.f * (f32)attr.b; } for (const auto& c : s) { error = FT_Load_Char(face, c, FT_LOAD_RENDER); if (error) continue; //SDL_Surface* surface = SDL_CreateRGBSurfaceFrom( // slot->bitmap.buffer, // slot->bitmap.width, // slot->bitmap.rows, // 8, // slot->bitmap.pitch, // 0, 0, 0, 255); //SDL_SetPaletteColors(surface->format->palette, colors, 0, 256); //SDL_Texture* glyph = SDL_CreateTextureFromSurface(renderer, surface); //SDL_SetTextureBlendMode(glyph, SDL_BlendMode::SDL_BLENDMODE_NONE); // SDL_SetTextureAlphaMod(glyph, attr.a); pos.x += slot->bitmap_left; pos.y -= slot->bitmap_top; //SDL_RenderCopy(renderer, glyph, nullptr, &pos); //SDL_DestroyTexture(glyph); //SDL_FreeSurface(surface); pos.x += slot->advance.x >> 6; pos.y += slot->advance.y >> 6; } } void RenderStringMarkdown(const NVL::Environment::Variable& s, u32 pos_x, u32 pos_y, const SDL_Color& attr) { NVL::String str = std::get(s.value); RenderString(str, pos_x, pos_y, attr); } }