Added parameter to load_font to load aliased or not

main
noah metz 2024-10-17 09:25:46 -06:00
parent 6858877d6b
commit c59c643cd0
3 changed files with 19 additions and 7 deletions

@ -129,6 +129,6 @@ typedef struct UIContextStruct {
VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, vec2 window_scale, VkRenderPass render_pass, Queue transfer_queue, VkCommandPool transfer_pool, UIContext* context);
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, uint32_t** charmap, FontDescriptor* descriptor);
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, uint32_t** charmap, FontDescriptor* descriptor);
#endif

@ -247,7 +247,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
FontDescriptor test_font;
uint32_t* charmap;
result = load_font(render_context->device, render_context->allocator, ui_context.font_layout, ui_context.font_pool, render_context->transfer_pool, render_context->transfer_queue, library, "test.ttf", 16, &charmap, &test_font);
result = load_font(render_context->device, render_context->allocator, ui_context.font_layout, ui_context.font_pool, render_context->transfer_pool, render_context->transfer_queue, library, "test.ttf", 16, VK_FALSE, &charmap, &test_font);
if(result != VK_SUCCESS) {
return result;
}

@ -459,7 +459,7 @@ VkResult create_font_descriptor_pools(VkDevice device, uint32_t max_sets, VkDesc
return VK_SUCCESS;
}
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, uint32_t** charmap, FontDescriptor* descriptor) {
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, uint32_t** charmap, FontDescriptor* descriptor) {
FT_Face face;
int error;
@ -482,6 +482,12 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
uint32_t max_width = 0;
uint32_t symbol_count = 0;
uint32_t c;
FT_Int32 load_flags = FT_LOAD_RENDER;
if(antialias == VK_TRUE) {
load_flags += FT_RENDER_MODE_NORMAL;
} else {
load_flags += FT_RENDER_MODE_MONO;
}
// TODO: worry about variants(that's why num_glyphs doesn't match symbol_count)
c = FT_Get_First_Char(face, &glyph_index);
@ -489,7 +495,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
if(glyph_index == 0) {
break;
}
FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_RENDER_MODE_MONO);
FT_Load_Glyph(face, glyph_index, load_flags);
uint32_t width = face->glyph->bitmap.width;
uint32_t height = face->glyph->bitmap.rows;
tmp_charmap[i] = c;
@ -516,15 +522,21 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
for(uint32_t i = 0; i < symbol_count; i++) {
glyph_index = FT_Get_Char_Index(face, (*charmap)[i]);
FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_RENDER_MODE_MONO);
FT_Load_Glyph(face, glyph_index, load_flags);
for(uint32_t y = 0; y < face->glyph->bitmap.rows; y++) {
for(uint32_t x = 0; x < face->glyph->bitmap.width; x++) {
if(antialias == VK_TRUE) {
uint32_t level = face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x];
level = ((level * 0xFFFFFF) / 256) << 8;
images[max_width*max_height*i + max_width*y + x] = 0x000000FF + level;
} else {
if(face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x] != 0) {
images[max_width*max_height*i + max_width*y + x] = 0xFFFFFFFF;
}
}
}
}
}
error = FT_Done_Face(face);