From c59c643cd0913d5374f9c931895733603af6ddec Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Thu, 17 Oct 2024 09:25:46 -0600 Subject: [PATCH] Added parameter to load_font to load aliased or not --- client/include/pipeline.h | 2 +- client/src/main.c | 2 +- client/src/pipeline.c | 22 +++++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/client/include/pipeline.h b/client/include/pipeline.h index bd70e73..0d8028c 100644 --- a/client/include/pipeline.h +++ b/client/include/pipeline.h @@ -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 diff --git a/client/src/main.c b/client/src/main.c index d431677..432713b 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -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; } diff --git a/client/src/pipeline.c b/client/src/pipeline.c index bf75324..3872738 100644 --- a/client/src/pipeline.c +++ b/client/src/pipeline.c @@ -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,11 +522,17 @@ 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(face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x] != 0) { - images[max_width*max_height*i + max_width*y + x] = 0xFFFFFFFF; + 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; + } } } }