mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:37:35 +00:00
SharedGraphics: Allow passing a Font to text drawing functions.
This way we don't have to juggle around with calls to Painter::set_font() which simplifies a bunch of places.
This commit is contained in:
parent
b8f999cbef
commit
ffe4522316
5 changed files with 63 additions and 29 deletions
|
@ -323,37 +323,42 @@ void Painter::blit(const Point& position, const GraphicsBitmap& source, const Re
|
|||
|
||||
[[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color)
|
||||
{
|
||||
draw_bitmap(point, font().glyph_bitmap(ch), color);
|
||||
draw_glyph(point, ch, font(), color);
|
||||
}
|
||||
|
||||
void Painter::draw_text(const Rect& rect, const char* text, int length, TextAlignment alignment, Color color)
|
||||
[[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, const Font& font, Color color)
|
||||
{
|
||||
draw_bitmap(point, font.glyph_bitmap(ch), color);
|
||||
}
|
||||
|
||||
void Painter::draw_text(const Rect& rect, const char* text, int length, const Font& font, TextAlignment alignment, Color color)
|
||||
{
|
||||
Point point;
|
||||
|
||||
if (alignment == TextAlignment::TopLeft) {
|
||||
point = rect.location();
|
||||
} else if (alignment == TextAlignment::CenterLeft) {
|
||||
point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) };
|
||||
point = { rect.x(), rect.center().y() - (font.glyph_height() / 2) };
|
||||
} else if (alignment == TextAlignment::CenterRight) {
|
||||
int text_width = font().width(text);
|
||||
point = { rect.right() - text_width, rect.center().y() - (font().glyph_height() / 2) };
|
||||
int text_width = font.width(text);
|
||||
point = { rect.right() - text_width, rect.center().y() - (font.glyph_height() / 2) };
|
||||
} else if (alignment == TextAlignment::Center) {
|
||||
int text_width = font().width(text);
|
||||
int text_width = font.width(text);
|
||||
point = rect.center();
|
||||
point.move_by(-(text_width / 2), -(font().glyph_height() / 2));
|
||||
point.move_by(-(text_width / 2), -(font.glyph_height() / 2));
|
||||
} else {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
int space_width = font().glyph_width(' ') + font().glyph_spacing();
|
||||
int space_width = font.glyph_width(' ') + font.glyph_spacing();
|
||||
for (ssize_t i = 0; i < length; ++i) {
|
||||
char ch = text[i];
|
||||
if (ch == ' ') {
|
||||
point.move_by(space_width, 0);
|
||||
continue;
|
||||
}
|
||||
draw_glyph(point, ch, color);
|
||||
point.move_by(font().glyph_width(ch) + font().glyph_spacing(), 0);
|
||||
draw_glyph(point, ch, font, color);
|
||||
point.move_by(font.glyph_width(ch) + font.glyph_spacing(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -362,6 +367,16 @@ void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alig
|
|||
draw_text(rect, text.characters(), text.length(), alignment, color);
|
||||
}
|
||||
|
||||
void Painter::draw_text(const Rect& rect, const String& text, const Font& font, TextAlignment alignment, Color color)
|
||||
{
|
||||
draw_text(rect, text.characters(), text.length(), font, alignment, color);
|
||||
}
|
||||
|
||||
void Painter::draw_text(const Rect& rect, const char* text, int length, TextAlignment alignment, Color color)
|
||||
{
|
||||
draw_text(rect, text, length, font(), alignment, color);
|
||||
}
|
||||
|
||||
void Painter::set_pixel(const Point& p, Color color)
|
||||
{
|
||||
auto point = p;
|
||||
|
|
|
@ -35,9 +35,12 @@ public:
|
|||
void blit(const Point&, const GraphicsBitmap&, const Rect& src_rect);
|
||||
void blit_with_opacity(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity);
|
||||
|
||||
void draw_text(const Rect&, const char* text, int length, TextAlignment = TextAlignment::TopLeft, Color = Color());
|
||||
void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
|
||||
void draw_text(const Rect&, const char* text, int length, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black);
|
||||
void draw_text(const Rect&, const char* text, int length, TextAlignment = TextAlignment::TopLeft, Color = Color::Black);
|
||||
void draw_text(const Rect&, const String&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black);
|
||||
void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black);
|
||||
void draw_glyph(const Point&, char, Color);
|
||||
void draw_glyph(const Point&, char, const Font&, Color);
|
||||
|
||||
const Font& font() const { return *state().font; }
|
||||
void set_font(const Font& font) { state().font = &font; }
|
||||
|
|
|
@ -179,8 +179,6 @@ WSWindowManager::WSWindowManager()
|
|||
m_front_painter = make<Painter>(*m_front_bitmap);
|
||||
m_back_painter = make<Painter>(*m_back_bitmap);
|
||||
|
||||
m_font = Font::default_font();
|
||||
|
||||
m_front_painter->set_font(font());
|
||||
m_back_painter->set_font(font());
|
||||
|
||||
|
@ -282,6 +280,26 @@ WSWindowManager::~WSWindowManager()
|
|||
{
|
||||
}
|
||||
|
||||
const Font& WSWindowManager::font() const
|
||||
{
|
||||
return Font::default_font();
|
||||
}
|
||||
|
||||
const Font& WSWindowManager::window_title_font() const
|
||||
{
|
||||
return Font::default_bold_font();
|
||||
}
|
||||
|
||||
const Font& WSWindowManager::menu_font() const
|
||||
{
|
||||
return Font::default_font();
|
||||
}
|
||||
|
||||
const Font& WSWindowManager::app_menu_font() const
|
||||
{
|
||||
return Font::default_bold_font();
|
||||
}
|
||||
|
||||
static void get_cpu_usage(unsigned& busy, unsigned& idle)
|
||||
{
|
||||
busy = 0;
|
||||
|
@ -463,9 +481,7 @@ void WSWindowManager::paint_window_frame(WSWindow& window)
|
|||
m_back_painter->draw_rect(outer_rect, border_color);
|
||||
m_back_painter->draw_rect(inner_border_rect, border_color);
|
||||
|
||||
m_back_painter->set_font(Font::default_bold_font());
|
||||
m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
|
||||
m_back_painter->set_font(font());
|
||||
m_back_painter->draw_text(titlebar_title_rect, window.title(), window_title_font(), TextAlignment::CenterLeft, title_color);
|
||||
|
||||
if (!s_close_button_bitmap)
|
||||
s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
|
||||
|
@ -946,11 +962,13 @@ void WSWindowManager::draw_menubar()
|
|||
m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
|
||||
text_color = Color::White;
|
||||
}
|
||||
if (index == 1)
|
||||
m_back_painter->set_font(Font::default_bold_font());
|
||||
m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
|
||||
if (index == 1)
|
||||
m_back_painter->set_font(font());
|
||||
m_back_painter->draw_text(
|
||||
menu.text_rect_in_menubar(),
|
||||
menu.name(),
|
||||
index == 1 ? app_menu_font() : menu_font(),
|
||||
TextAlignment::CenterLeft,
|
||||
text_color
|
||||
);
|
||||
++index;
|
||||
return true;
|
||||
});
|
||||
|
|
|
@ -70,8 +70,10 @@ public:
|
|||
void recompose_immediately();
|
||||
void flush(const Rect&);
|
||||
|
||||
Font& font() { return *m_font; }
|
||||
const Font& font() const { return *m_font; }
|
||||
const Font& font() const;
|
||||
const Font& window_title_font() const;
|
||||
const Font& menu_font() const;
|
||||
const Font& app_menu_font() const;
|
||||
|
||||
void close_menu(WSMenu&);
|
||||
void close_menubar(WSMenuBar&);
|
||||
|
@ -154,8 +156,6 @@ private:
|
|||
OwnPtr<Painter> m_back_painter;
|
||||
OwnPtr<Painter> m_front_painter;
|
||||
|
||||
RetainPtr<Font> m_font;
|
||||
|
||||
String m_wallpaper_path;
|
||||
RetainPtr<GraphicsBitmap> m_wallpaper;
|
||||
|
||||
|
|
|
@ -81,9 +81,7 @@ void WSWindowSwitcher::draw()
|
|||
rect_text_color = Color::DarkGray;
|
||||
}
|
||||
painter.blit(item_rect.location().translated(0, (item_rect.height() - window.icon().height()) / 2), window.icon(), window.icon().rect());
|
||||
painter.set_font(Font::default_bold_font());
|
||||
painter.draw_text(item_rect.translated(window.icon().width() + 4, 0), window.title(), TextAlignment::CenterLeft, text_color);
|
||||
painter.set_font(WSWindowManager::the().font());
|
||||
painter.draw_text(item_rect.translated(window.icon().width() + 4, 0), window.title(), WSWindowManager::the().window_title_font(), TextAlignment::CenterLeft, text_color);
|
||||
painter.draw_text(item_rect, window.rect().to_string(), TextAlignment::CenterRight, rect_text_color);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue