Handle multiple font styles

Adds support for font styles other than normal, such as bold, italics,
and bold italics.
This commit is contained in:
Leroy Hopson 2024-02-10 17:15:33 +13:00
parent fb3fb6855c
commit 3f24fa9bf0
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
2 changed files with 30 additions and 10 deletions

View file

@ -232,11 +232,12 @@ int Terminal::_draw_cb(struct tsm_screen *con,
// Erase any previous character in the cell. // Erase any previous character in the cell.
term->rs->canvas_item_add_rect(term->char_canvas_item, cell_rect, Color(1, 1, 1, 0)); term->rs->canvas_item_add_rect(term->char_canvas_item, cell_rect, Color(1, 1, 1, 0));
term->font->draw_char( FontType font_type = static_cast<FontType>((attr->bold ? 1 : 0) | (attr->italic ? 2 : 0));
Ref<Font> font = term->fonts[font_type];
font->draw_char(
term->char_canvas_item, term->char_canvas_item,
Vector2i(cell_position.x, cell_position.y + term->font->get_height( Vector2i(cell_position.x, cell_position.y + term->font_offset),
term->font_size /
1.25)),
String((char *)ch).unicode_at(0), String((char *)ch).unicode_at(0),
term->font_size, term->font_size,
fgcol fgcol
@ -347,9 +348,14 @@ void Terminal::update_sizes(bool force)
uint prev_rows = rows; uint prev_rows = rows;
size = get_size(); size = get_size();
Ref<Font> font = fonts[FontType::NORMAL];
font_size = get_theme_font_size("font_size"); font_size = get_theme_font_size("font_size");
cell_size = get_theme_font("normal_font")->get_string_size( font_offset = font->get_height(font_size / 1.25);
"W", HORIZONTAL_ALIGNMENT_LEFT, -1, get_theme_font_size("font_size"));
cell_size = font->get_string_size(
"W", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
cols = floor(size.x / cell_size.x); cols = floor(size.x / cell_size.x);
rows = floor(size.y / cell_size.y); rows = floor(size.y / cell_size.y);
@ -439,6 +445,7 @@ void Terminal::initialize_rendering() {
} }
void Terminal::update_theme() { void Terminal::update_theme() {
// Update colors.
palette.resize(TSM_COLOR_NUM); palette.resize(TSM_COLOR_NUM);
for (int i = 0; i < TSM_COLOR_NUM; i++) { for (int i = 0; i < TSM_COLOR_NUM; i++) {
tsm_vte_color color = static_cast<tsm_vte_color>(i); tsm_vte_color color = static_cast<tsm_vte_color>(i);
@ -446,8 +453,11 @@ void Terminal::update_theme() {
} }
back_material->set_shader_parameter("background_color", palette[TSM_COLOR_BACKGROUND]); back_material->set_shader_parameter("background_color", palette[TSM_COLOR_BACKGROUND]);
// TODO: Default to mono font and handle other styles. // Update fonts.
font = get_theme_font("normal_font"); for (int i = FontType::NORMAL; i <= FontType::BOLD_ITALICS; i++) {
FontType type = static_cast<FontType>(i);
fonts[type] = has_theme_font(FONT_TYPES[type]) ? get_theme_font(FONT_TYPES[type]) : get_theme_font(FONT_TYPES[FontType::NORMAL]);
}
refresh(); refresh();
} }

View file

@ -3,6 +3,8 @@
#pragma once #pragma once
#include<map>
#include <godot_cpp/classes/control.hpp> #include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/image_texture.hpp> #include <godot_cpp/classes/image_texture.hpp>
#include <godot_cpp/classes/rendering_server.hpp> #include <godot_cpp/classes/rendering_server.hpp>
@ -27,6 +29,13 @@ namespace godot
"normal_font", "bold_font", "italics_font", "bold_italics_font", "normal_font", "bold_font", "italics_font", "bold_italics_font",
}; };
enum FontType {
NORMAL,
BOLD,
ITALICS,
BOLD_ITALICS,
};
public: public:
enum AttrFlag enum AttrFlag
{ {
@ -92,8 +101,9 @@ namespace godot
tsm_age_t age, void *data); tsm_age_t age, void *data);
PackedColorArray palette; PackedColorArray palette;
Ref<Font> font; std::map<FontType, Ref<Font>> fonts;
int32_t font_size; int32_t font_size;
double font_offset;
Vector2 size; Vector2 size;
Vector2 cell_size; Vector2 cell_size;