summaryrefslogtreecommitdiff
path: root/src/vk/texture.cpp
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2023-04-24 13:32:39 -0300
committerFrederico Linhares <fred@linhares.blue>2023-04-24 13:32:39 -0300
commit959b717b46c00930a66fb8959d3469c0b63f3e66 (patch)
treeff03dc62b3b06929af4545c02d385320828b2229 /src/vk/texture.cpp
parent66cb556fb6f87d195aacf8a25ffafb86d524da19 (diff)
fixt Improve text rendering
Diffstat (limited to 'src/vk/texture.cpp')
-rw-r--r--src/vk/texture.cpp35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/vk/texture.cpp b/src/vk/texture.cpp
index 93bfe61..c699ecc 100644
--- a/src/vk/texture.cpp
+++ b/src/vk/texture.cpp
@@ -247,12 +247,11 @@ const CommandChain image_loader{
struct CharacterToDraw
{
- int pos_x, pos_y;
+ int pos_x;
std::shared_ptr<VK::Character> character;
- CharacterToDraw(int x, int y, std::shared_ptr<VK::Character> character):
+ CharacterToDraw(int x, std::shared_ptr<VK::Character> character):
pos_x{x},
- pos_y{y},
character{character}
{};
};
@@ -261,6 +260,7 @@ struct TextTextureBuilder: public ImageBuilder
{
VK::Font *font;
const char* str;
+ uint32_t max_bearing_y;
std::vector<CharacterToDraw> chars_to_draw;
TextTextureBuilder(VK::Texture *texture, VK::Font *font, const char* str):
@@ -276,31 +276,35 @@ load_text_proportions(void *obj)
{
auto self = static_cast<TextTextureBuilder*>(obj);
- uint32_t texture_width, texture_height;
+ uint32_t texture_width{0}, texture_descender{0};
auto unicode_text{VK::Character::str_to_unicode(self->str)};
- texture_width = 0;
- texture_height = 0;
+ auto first_image{self->font->character(unicode_text[0])};
+ if(first_image->bearing_x < 0) texture_width = - first_image->bearing_x;
+
+ self->max_bearing_y = 0;
self->chars_to_draw.reserve(unicode_text.size());
{ // Calculate image size
int max_height;
for(auto char_code : unicode_text)
{
- auto char_image = self->font->character(char_code);
+ auto char_image{self->font->character(char_code)};
+ uint32_t descender{char_image->height - char_image->bearing_y};
uint32_t pos_x{texture_width + char_image->bearing_x};
- uint32_t pos_y{char_image->height - char_image->bearing_y};
- self->chars_to_draw.emplace_back(pos_x, pos_y, char_image);
+ if(char_image->image != VK_NULL_HANDLE)
+ self->chars_to_draw.emplace_back(pos_x, char_image);
- if(char_image->height > texture_height)
- texture_height = char_image->height;
+ if(char_image->bearing_y > self->max_bearing_y)
+ self->max_bearing_y = char_image->bearing_y;
+ if(descender > texture_descender) texture_descender = descender;
texture_width += char_image->advance;
}
}
self->texture->width = texture_width;
- self->texture->height = texture_height;
+ self->texture->height = self->max_bearing_y + texture_descender;
self->texture->mip_levels = 1;
}
@@ -323,7 +327,7 @@ load_text_image(void *obj)
pixels[image_coord] = 0; // Red
pixels[image_coord + 1] = 0; // Green
pixels[image_coord + 2] = 0; // Blue
- pixels[image_coord + 3] = 255; // Alpha
+ pixels[image_coord + 3] = 0; // Alpha
}
}
VK::SourceBuffer source_image_buffer{
@@ -392,7 +396,10 @@ load_text_image(void *obj)
image_copy.srcSubresource = source_subresources;
image_copy.srcOffset = {0, 0, 0};
image_copy.dstSubresource = destination_subresources;
- image_copy.dstOffset = {to_draw.pos_x, to_draw.pos_y, 0};
+ image_copy.dstOffset = {
+ to_draw.pos_x,
+ (int)(self->max_bearing_y - to_draw.character->bearing_y),
+ 0};
image_copy.extent = {
to_draw.character->width, to_draw.character->height, 1};