mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:52:45 +00:00 
			
		
		
		
	Make a preparation pass for variable-width fonts.
This commit is contained in:
		
							parent
							
								
									b85fe0bd07
								
							
						
					
					
						commit
						0a86366c71
					
				
					 9 changed files with 72 additions and 32 deletions
				
			
		|  | @ -86,7 +86,7 @@ GlyphMapWidget::~GlyphMapWidget() | |||
| 
 | ||||
| int GlyphMapWidget::preferred_width() const | ||||
| { | ||||
|     return columns() * (font().glyph_width() + m_horizontal_spacing) + 2; | ||||
|     return columns() * (font().max_glyph_width() + m_horizontal_spacing) + 2; | ||||
| } | ||||
| 
 | ||||
| int GlyphMapWidget::preferred_height() const | ||||
|  | @ -109,9 +109,9 @@ Rect GlyphMapWidget::get_outer_rect(byte glyph) const | |||
|     int row = glyph / columns(); | ||||
|     int column = glyph % columns(); | ||||
|     return { | ||||
|         column * (font().glyph_width() + m_horizontal_spacing) + 1, | ||||
|         column * (font().max_glyph_width() + m_horizontal_spacing) + 1, | ||||
|         row * (font().glyph_height() + m_vertical_spacing) + 1, | ||||
|         font().glyph_width() + m_horizontal_spacing, | ||||
|         font().max_glyph_width() + m_horizontal_spacing, | ||||
|         font().glyph_height() + m_horizontal_spacing | ||||
|     }; | ||||
| } | ||||
|  | @ -131,7 +131,7 @@ void GlyphMapWidget::paint_event(GPaintEvent&) | |||
|             Rect inner_rect( | ||||
|                 outer_rect.x() + m_horizontal_spacing / 2, | ||||
|                 outer_rect.y() + m_vertical_spacing / 2, | ||||
|                 font().glyph_width(), | ||||
|                 font().max_glyph_width(), | ||||
|                 font().glyph_height() | ||||
|             ); | ||||
|             if (glyph == m_selected_glyph) { | ||||
|  | @ -184,9 +184,9 @@ void GlyphEditorWidget::paint_event(GPaintEvent&) | |||
|     painter.draw_rect(rect(), Color::Black); | ||||
| 
 | ||||
|     for (int y = 0; y < font().glyph_height(); ++y) | ||||
|         painter.draw_line({ 0, y * m_scale }, { font().glyph_width() * m_scale, y * m_scale }, Color::Black); | ||||
|         painter.draw_line({ 0, y * m_scale }, { font().max_glyph_width() * m_scale, y * m_scale }, Color::Black); | ||||
| 
 | ||||
|     for (int x = 0; x < font().glyph_width(); ++x) | ||||
|     for (int x = 0; x < font().max_glyph_width(); ++x) | ||||
|         painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, Color::Black); | ||||
| 
 | ||||
|     painter.translate(1, 1); | ||||
|  | @ -194,7 +194,7 @@ void GlyphEditorWidget::paint_event(GPaintEvent&) | |||
|     auto bitmap = font().glyph_bitmap(m_glyph); | ||||
| 
 | ||||
|     for (int y = 0; y < font().glyph_height(); ++y) { | ||||
|         for (int x = 0; x < font().glyph_width(); ++x) { | ||||
|         for (int x = 0; x < font().max_glyph_width(); ++x) { | ||||
|             Rect rect { x * m_scale, y * m_scale, m_scale, m_scale }; | ||||
|             if (bitmap.bit_at(x, y)) | ||||
|                 painter.fill_rect(rect, Color::Black); | ||||
|  | @ -239,7 +239,7 @@ void GlyphEditorWidget::draw_at_mouse(const GMouseEvent& event) | |||
| 
 | ||||
| int GlyphEditorWidget::preferred_width() const | ||||
| { | ||||
|     return font().glyph_width() * m_scale + 1; | ||||
|     return font().max_glyph_width() * m_scale + 1; | ||||
| } | ||||
| 
 | ||||
| int GlyphEditorWidget::preferred_height() const | ||||
|  |  | |||
|  | @ -619,7 +619,7 @@ void Terminal::set_size(word columns, word rows) | |||
|     for (size_t i = 0; i < rows; ++i) | ||||
|         m_lines[i] = new Line(columns); | ||||
| 
 | ||||
|     m_pixel_width = m_columns * font().glyph_width() + m_inset * 2; | ||||
|     m_pixel_width = m_columns * font().glyph_width('x') + m_inset * 2; | ||||
|     m_pixel_height = (m_rows * (font().glyph_height() + m_line_spacing)) + (m_inset * 2) - m_line_spacing; | ||||
| 
 | ||||
|     set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); | ||||
|  | @ -639,14 +639,14 @@ void Terminal::set_size(word columns, word rows) | |||
| Rect Terminal::glyph_rect(word row, word column) | ||||
| { | ||||
|     int y = row * m_line_height; | ||||
|     int x = column * font().glyph_width(); | ||||
|     return { x + m_inset, y + m_inset, font().glyph_width(), font().glyph_height() }; | ||||
|     int x = column * font().glyph_width('x'); | ||||
|     return { x + m_inset, y + m_inset, font().glyph_width('x'), font().glyph_height() }; | ||||
| } | ||||
| 
 | ||||
| Rect Terminal::row_rect(word row) | ||||
| { | ||||
|     int y = row * m_line_height; | ||||
|     Rect rect = { m_inset, y + m_inset, font().glyph_width() * m_columns, font().glyph_height() }; | ||||
|     Rect rect = { m_inset, y + m_inset, font().glyph_width('x') * m_columns, font().glyph_height() }; | ||||
|     rect.inflate(0, m_line_spacing); | ||||
|     return rect; | ||||
| } | ||||
|  | @ -798,13 +798,13 @@ void Terminal::force_repaint() | |||
| 
 | ||||
| void Terminal::resize_event(GResizeEvent& event) | ||||
| { | ||||
|     int new_columns = event.size().width() / m_font->glyph_width(); | ||||
|     int new_columns = event.size().width() / m_font->glyph_width('x'); | ||||
|     int new_rows = event.size().height() / m_line_height; | ||||
|     set_size(new_columns, new_rows); | ||||
| } | ||||
| 
 | ||||
| void Terminal::apply_size_increments_to_window(GWindow& window) | ||||
| { | ||||
|     window.set_size_increment({ font().glyph_width(), m_line_height }); | ||||
|     window.set_size_increment({ font().glyph_width('x'), m_line_height }); | ||||
|     window.set_base_size({ m_inset * 2, m_inset * 2}); | ||||
| } | ||||
|  |  | |||
|  | @ -151,4 +151,6 @@ private: | |||
| 
 | ||||
|     float m_opacity { 0.8f }; | ||||
|     bool m_needs_background_fill { true }; | ||||
| 
 | ||||
|     int m_glyph_width { 0 }; | ||||
| }; | ||||
|  |  | |||
|  | @ -26,6 +26,7 @@ void GTextBox::set_text(const String& text) | |||
| 
 | ||||
| void GTextBox::paint_event(GPaintEvent& event) | ||||
| { | ||||
|     ASSERT(font().is_fixed_width()); | ||||
|     Painter painter(*this); | ||||
|     painter.set_clip_rect(event.rect()); | ||||
| 
 | ||||
|  | @ -38,23 +39,27 @@ void GTextBox::paint_event(GPaintEvent& event) | |||
|     Rect inner_rect = rect(); | ||||
|     inner_rect.shrink(6, 6); | ||||
| 
 | ||||
|     ssize_t max_chars_to_paint = inner_rect.width() / font().glyph_width(); | ||||
|     ssize_t max_chars_to_paint = inner_rect.width() / font().min_glyph_width(); | ||||
| 
 | ||||
|     int first_visible_char = max((int)m_cursor_position - (int)max_chars_to_paint, 0); | ||||
|     ssize_t chars_to_paint = min(m_text.length() - first_visible_char, max_chars_to_paint); | ||||
| 
 | ||||
|     int y = inner_rect.center().y() - font().glyph_height() / 2;     | ||||
|     int space_width = font().glyph_width(' '); | ||||
|     int x = inner_rect.x(); | ||||
| 
 | ||||
|     for (ssize_t i = 0; i < chars_to_paint; ++i) { | ||||
|         char ch = m_text[first_visible_char + i]; | ||||
|         if (ch == ' ') | ||||
|         if (ch == ' ') { | ||||
|             x += space_width; | ||||
|             continue; | ||||
|         int x = inner_rect.x() + (i * font().glyph_width()); | ||||
|         } | ||||
|         painter.draw_glyph({x, y}, ch, Color::Black); | ||||
|         x += font().glyph_width(ch); | ||||
|     } | ||||
| 
 | ||||
|     if (is_focused() && m_cursor_blink_state) { | ||||
|         unsigned visible_cursor_position = m_cursor_position - first_visible_char; | ||||
|         Rect cursor_rect(inner_rect.x() + visible_cursor_position * font().glyph_width(), inner_rect.y(), 1, inner_rect.height()); | ||||
|         int visible_cursor_position = m_cursor_position - first_visible_char; | ||||
|         Rect cursor_rect(inner_rect.x() + visible_cursor_position * font().glyph_width('x'), inner_rect.y(), 1, inner_rect.height()); | ||||
|         painter.fill_rect(cursor_rect, foreground_color()); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -55,7 +55,16 @@ Font::Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_heig | |||
|     , m_rows(rows) | ||||
|     , m_glyph_width(glyph_width) | ||||
|     , m_glyph_height(glyph_height) | ||||
|     , m_min_glyph_width(glyph_width) | ||||
|     , m_max_glyph_width(glyph_width) | ||||
| { | ||||
|     m_fixed_width = true; | ||||
|     if (!m_fixed_width) { | ||||
|         byte minimum = 255; | ||||
|         for (int i = 0; i < 256; ++i) | ||||
|             minimum = min(minimum, m_glyph_widths[i]); | ||||
|         m_min_glyph_width = minimum; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| Font::~Font() | ||||
|  | @ -137,3 +146,14 @@ bool Font::write_to_file(const String& path) | |||
|     ASSERT(rc == 0); | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| int Font::width(const String& string) const | ||||
| { | ||||
|     if (m_fixed_width) | ||||
|         return string.length() * m_glyph_width; | ||||
| 
 | ||||
|     int width = 0; | ||||
|     for (int i = 0; i < string.length(); ++i) | ||||
|         width += glyph_width(string[i]); | ||||
|     return width; | ||||
| } | ||||
|  |  | |||
|  | @ -54,21 +54,30 @@ public: | |||
| 
 | ||||
|     GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(byte)ch * m_glyph_height], { m_glyph_width, m_glyph_height }); } | ||||
| 
 | ||||
|     byte glyph_width() const { return m_glyph_width; } | ||||
|     byte glyph_width(char ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[(byte)ch]; } | ||||
|     byte glyph_height() const { return m_glyph_height; } | ||||
|     int width(const String& string) const { return string.length() * glyph_width(); } | ||||
|     byte min_glyph_width() const { return m_min_glyph_width; } | ||||
|     byte max_glyph_width() const { return m_max_glyph_width; } | ||||
|     int width(const String& string) const; | ||||
| 
 | ||||
|     String name() const { return m_name; } | ||||
|     void set_name(const String& name) { m_name = name; } | ||||
| 
 | ||||
|     bool is_fixed_width() const { return m_fixed_width; } | ||||
| 
 | ||||
| private: | ||||
|     Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height); | ||||
| 
 | ||||
|     String m_name; | ||||
| 
 | ||||
|     unsigned* m_rows { nullptr }; | ||||
|     byte* m_glyph_widths { nullptr }; | ||||
|     void* m_mmap_ptr { nullptr }; | ||||
| 
 | ||||
|     byte m_glyph_width { 0 }; | ||||
|     byte m_glyph_height { 0 }; | ||||
|     byte m_min_glyph_width { 0 }; | ||||
|     byte m_max_glyph_width { 0 }; | ||||
| 
 | ||||
|     bool m_fixed_width { false }; | ||||
| }; | ||||
|  |  | |||
|  | @ -332,21 +332,25 @@ void Painter::draw_text(const Rect& rect, const String& text, TextAlignment alig | |||
|     } else if (alignment == TextAlignment::CenterLeft) { | ||||
|         point = { rect.x(), rect.center().y() - (font().glyph_height() / 2) }; | ||||
|     } else if (alignment == TextAlignment::CenterRight) { | ||||
|         int text_width = text.length() * font().glyph_width(); | ||||
|         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 = text.length() * font().glyph_width(); | ||||
|         int text_width = font().width(text); | ||||
|         point = rect.center(); | ||||
|         point.move_by(-(text_width / 2), -(font().glyph_height() / 2)); | ||||
|     } else { | ||||
|         ASSERT_NOT_REACHED(); | ||||
|     } | ||||
| 
 | ||||
|     for (ssize_t i = 0; i < text.length(); ++i, point.move_by(font().glyph_width(), 0)) { | ||||
|     int space_width = font().glyph_width(' '); | ||||
|     for (ssize_t i = 0; i < text.length(); ++i) { | ||||
|         byte ch = text[i]; | ||||
|         if (ch == ' ') | ||||
|         if (ch == ' ') { | ||||
|             point.move_by(space_width, 0); | ||||
|             continue; | ||||
|         } | ||||
|         draw_glyph(point, ch, color); | ||||
|         point.move_by(font().glyph_width(ch), 0); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -404,7 +404,7 @@ void WSWindowManager::paint_window_frame(WSWindow& window) | |||
|     auto close_button_rect = close_button_rect_for_window(window.rect()); | ||||
| 
 | ||||
|     auto titlebar_title_rect = titlebar_inner_rect; | ||||
|     titlebar_title_rect.set_width(font().glyph_width() * window.title().length()); | ||||
|     titlebar_title_rect.set_width(font().width(window.title())); | ||||
| 
 | ||||
|     Rect inner_border_rect { | ||||
|         window.x() - 1, | ||||
|  | @ -948,7 +948,7 @@ void WSWindowManager::draw_menubar() | |||
|     auto time_rect = menubar_rect().translated(-(menubar_menu_margin() / 2), 0); | ||||
|     m_back_painter->draw_text(time_rect, time_text, TextAlignment::CenterRight, Color::Black); | ||||
| 
 | ||||
|     Rect cpu_rect { time_rect.right() - font().glyph_width() * time_text.length() - (int)m_cpu_history.capacity() - 10, time_rect.y() + 1, (int)m_cpu_history.capacity(), time_rect.height() - 2 }; | ||||
|     Rect cpu_rect { time_rect.right() - font().width(time_text) - (int)m_cpu_history.capacity() - 10, time_rect.y() + 1, (int)m_cpu_history.capacity(), time_rect.height() - 2 }; | ||||
|     m_back_painter->fill_rect(cpu_rect, Color::Black); | ||||
|     int i = m_cpu_history.capacity() - m_cpu_history.size(); | ||||
|     for (auto cpu_usage : m_cpu_history) { | ||||
|  |  | |||
|  | @ -95,10 +95,10 @@ void WSWindowSwitcher::refresh() | |||
|     m_windows.clear(); | ||||
|     m_selected_index = 0; | ||||
|     int window_count = 0; | ||||
|     int longest_title = 0; | ||||
|     int longest_title_width = 0; | ||||
|     WSWindowManager::the().for_each_visible_window_of_type_from_back_to_front(WSWindowType::Normal, [&] (WSWindow& window) { | ||||
|         ++window_count; | ||||
|         longest_title = max(longest_title, window.title().length()); | ||||
|         longest_title_width = max(longest_title_width, WSWindowManager::the().font().width(window.title())); | ||||
|         if (selected_window == &window) | ||||
|             m_selected_index = m_windows.size(); | ||||
|         m_windows.append(window.make_weak_ptr()); | ||||
|  | @ -108,8 +108,8 @@ void WSWindowSwitcher::refresh() | |||
|         hide(); | ||||
|         return; | ||||
|     } | ||||
|     int space_for_window_rect = WSWindowManager::the().font().glyph_width() * 24; | ||||
|     m_rect.set_width(longest_title * WSWindowManager::the().font().glyph_width() + space_for_window_rect + padding() * 2); | ||||
|     int space_for_window_rect = 180; | ||||
|     m_rect.set_width(longest_title_width + space_for_window_rect + padding() * 2); | ||||
|     m_rect.set_height(window_count * item_height() + padding() * 2); | ||||
|     m_rect.center_within(WSWindowManager::the().m_screen_rect); | ||||
|     if (!m_switcher_window) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling