From a35693915e909cf07eb3eb4bde41022cbea70149 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 28 Dec 2020 15:51:43 +0100 Subject: [PATCH] LibGUI: Make syntax highlighter communicate boldness instead of font Now that fonts know their own weight, we no longer need highlighters to tell us which font to use. Instead, they can just say "this should be bold" and we'll find the right font ourselves. --- .../Spreadsheet/CellSyntaxHighlighter.cpp | 2 +- Libraries/LibGUI/CppSyntaxHighlighter.cpp | 22 +++++++++---------- Libraries/LibGUI/GMLSyntaxHighlighter.cpp | 4 ++-- Libraries/LibGUI/INISyntaxHighlighter.cpp | 6 ++--- Libraries/LibGUI/JSSyntaxHighlighter.cpp | 6 ++--- Libraries/LibGUI/ShellSyntaxHighlighter.cpp | 16 +++++++------- Libraries/LibGUI/SyntaxHighlighter.h | 4 ++-- Libraries/LibGUI/TextDocument.h | 2 +- Libraries/LibGUI/TextEditor.cpp | 9 +++++--- 9 files changed, 37 insertions(+), 34 deletions(-) diff --git a/Applications/Spreadsheet/CellSyntaxHighlighter.cpp b/Applications/Spreadsheet/CellSyntaxHighlighter.cpp index aef6eac21f..8a3c0a5688 100644 --- a/Applications/Spreadsheet/CellSyntaxHighlighter.cpp +++ b/Applications/Spreadsheet/CellSyntaxHighlighter.cpp @@ -51,7 +51,7 @@ void CellSyntaxHighlighter::rehighlight(Gfx::Palette palette) Optional {}, false, false, - nullptr, + false, nullptr); } diff --git a/Libraries/LibGUI/CppSyntaxHighlighter.cpp b/Libraries/LibGUI/CppSyntaxHighlighter.cpp index 8b391f16c0..03c2e6ff59 100644 --- a/Libraries/LibGUI/CppSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/CppSyntaxHighlighter.cpp @@ -36,29 +36,29 @@ static TextStyle style_for_token_type(Gfx::Palette palette, Cpp::Token::Type typ { switch (type) { case Cpp::Token::Type::Keyword: - return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_keyword(), true }; case Cpp::Token::Type::KnownType: - return { palette.syntax_type(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_type(), true }; case Cpp::Token::Type::Identifier: - return { palette.syntax_identifier() }; + return { palette.syntax_identifier(), false }; case Cpp::Token::Type::DoubleQuotedString: case Cpp::Token::Type::SingleQuotedString: case Cpp::Token::Type::RawString: - return { palette.syntax_string() }; + return { palette.syntax_string(), false }; case Cpp::Token::Type::Integer: case Cpp::Token::Type::Float: - return { palette.syntax_number() }; + return { palette.syntax_number(), false }; case Cpp::Token::Type::IncludePath: - return { palette.syntax_preprocessor_value() }; + return { palette.syntax_preprocessor_value(), false }; case Cpp::Token::Type::EscapeSequence: - return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_keyword(), true }; case Cpp::Token::Type::PreprocessorStatement: case Cpp::Token::Type::IncludeStatement: - return { palette.syntax_preprocessor_statement() }; + return { palette.syntax_preprocessor_statement(), false }; case Cpp::Token::Type::Comment: - return { palette.syntax_comment() }; + return { palette.syntax_comment(), false }; default: - return { palette.base_text() }; + return { palette.base_text(), false }; } } @@ -91,7 +91,7 @@ void CppSyntaxHighlighter::rehighlight(Gfx::Palette palette) span.range.set_end({ token.m_end.line, token.m_end.column }); auto style = style_for_token_type(palette, token.m_type); span.color = style.color; - span.font = style.font; + span.bold = style.bold; span.is_skippable = token.m_type == Cpp::Token::Type::Whitespace; span.data = reinterpret_cast(token.m_type); spans.append(span); diff --git a/Libraries/LibGUI/GMLSyntaxHighlighter.cpp b/Libraries/LibGUI/GMLSyntaxHighlighter.cpp index eea5aa3b16..a7616b2456 100644 --- a/Libraries/LibGUI/GMLSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/GMLSyntaxHighlighter.cpp @@ -41,7 +41,7 @@ static TextStyle style_for_token_type(Gfx::Palette palette, GMLToken::Type type) case GMLToken::Type::ClassMarker: return { palette.syntax_keyword() }; case GMLToken::Type::ClassName: - return { palette.syntax_identifier(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_identifier(), true }; case GMLToken::Type::Identifier: return { palette.syntax_identifier() }; case GMLToken::Type::JsonValue: @@ -73,7 +73,7 @@ void GMLSyntaxHighlighter::rehighlight(Gfx::Palette palette) span.range.set_end({ token.m_end.line, token.m_end.column }); auto style = style_for_token_type(palette, token.m_type); span.color = style.color; - span.font = style.font; + span.bold = style.bold; span.is_skippable = false; span.data = reinterpret_cast(token.m_type); spans.append(span); diff --git a/Libraries/LibGUI/INISyntaxHighlighter.cpp b/Libraries/LibGUI/INISyntaxHighlighter.cpp index 4ce3f1ac42..e5aab5c49e 100644 --- a/Libraries/LibGUI/INISyntaxHighlighter.cpp +++ b/Libraries/LibGUI/INISyntaxHighlighter.cpp @@ -38,7 +38,7 @@ static TextStyle style_for_token_type(Gfx::Palette palette, IniToken::Type type) case IniToken::Type::LeftBracket: case IniToken::Type::RightBracket: case IniToken::Type::section: - return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_keyword(), true }; case IniToken::Type::Name: return { palette.syntax_identifier() }; case IniToken::Type::Value: @@ -46,7 +46,7 @@ static TextStyle style_for_token_type(Gfx::Palette palette, IniToken::Type type) case IniToken::Type::Comment: return { palette.syntax_comment() }; case IniToken::Type::Equal: - return { palette.syntax_operator(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_operator(), true }; default: return { palette.base_text() }; } @@ -72,7 +72,7 @@ void IniSyntaxHighlighter::rehighlight(Gfx::Palette palette) span.range.set_end({ token.m_end.line, token.m_end.column }); auto style = style_for_token_type(palette, token.m_type); span.color = style.color; - span.font = style.font; + span.bold = style.bold; span.is_skippable = token.m_type == IniToken::Type::Whitespace; span.data = reinterpret_cast(token.m_type); spans.append(span); diff --git a/Libraries/LibGUI/JSSyntaxHighlighter.cpp b/Libraries/LibGUI/JSSyntaxHighlighter.cpp index d2843897b9..16bec63b50 100644 --- a/Libraries/LibGUI/JSSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/JSSyntaxHighlighter.cpp @@ -47,9 +47,9 @@ static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type) case JS::TokenCategory::Operator: return { palette.syntax_operator() }; case JS::TokenCategory::Keyword: - return { palette.syntax_keyword(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_keyword(), true }; case JS::TokenCategory::ControlKeyword: - return { palette.syntax_control_keyword(), &Gfx::Font::default_bold_fixed_width_font() }; + return { palette.syntax_control_keyword(), true }; case JS::TokenCategory::Identifier: return { palette.syntax_identifier() }; default: @@ -101,7 +101,7 @@ void JSSyntaxHighlighter::rehighlight(Gfx::Palette palette) auto type = is_trivia ? JS::TokenType::Invalid : token.type(); auto style = style_for_token_type(palette, type); span.color = style.color; - span.font = style.font; + span.bold = style.bold; span.is_skippable = is_trivia; span.data = reinterpret_cast(static_cast(type)); spans.append(span); diff --git a/Libraries/LibGUI/ShellSyntaxHighlighter.cpp b/Libraries/LibGUI/ShellSyntaxHighlighter.cpp index e3d31fe940..3fe1fd4738 100644 --- a/Libraries/LibGUI/ShellSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/ShellSyntaxHighlighter.cpp @@ -125,7 +125,7 @@ private: span.range.set_start({ node->and_position().start_line.line_number, node->and_position().start_line.line_column }); set_offset_range_end(span.range, node->and_position().end_line); span.color = m_palette.syntax_punctuation(); - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; } virtual void visit(const AST::ListConcatenate* node) override { @@ -138,7 +138,7 @@ private: auto& span = span_for_node(node); set_offset_range_start(span.range, node->position().end_line); span.color = m_palette.syntax_punctuation(); - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; } virtual void visit(const AST::BraceExpansion* node) override { @@ -151,7 +151,7 @@ private: auto& span = span_for_node(node); if (m_is_first_in_command) { span.color = m_palette.syntax_keyword(); - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; m_is_first_in_command = false; } else if (node->text().starts_with("-")) { span.color = m_palette.syntax_preprocessor_statement(); @@ -213,8 +213,8 @@ private: end_span.is_skippable = true; if (m_is_first_in_command) { - start_span.font = &Gfx::Font::default_bold_fixed_width_font(); - end_span.font = &Gfx::Font::default_bold_fixed_width_font(); + start_span.bold = true; + end_span.bold = true; } m_is_first_in_command = false; } @@ -348,7 +348,7 @@ private: span.range.set_start({ node->or_position().start_line.line_number, node->or_position().start_line.line_column }); set_offset_range_end(span.range, node->or_position().end_line); span.color = m_palette.syntax_punctuation(); - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; } virtual void visit(const AST::Pipe* node) override { @@ -386,7 +386,7 @@ private: span.range.set_start({ node->separator_position().start_line.line_number, node->separator_position().start_line.line_column }); set_offset_range_end(span.range, node->separator_position().end_line); span.color = m_palette.syntax_punctuation(); - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; span.is_skippable = true; } virtual void visit(const AST::Subshell* node) override @@ -421,7 +421,7 @@ private: auto& span = span_for_node(node); span.color = m_palette.syntax_string(); if (m_is_first_in_command) - span.font = &Gfx::Font::default_bold_fixed_width_font(); + span.bold = true; m_is_first_in_command = false; } virtual void visit(const AST::StringPartCompose* node) override diff --git a/Libraries/LibGUI/SyntaxHighlighter.h b/Libraries/LibGUI/SyntaxHighlighter.h index 3b59de8097..1ab1f51c42 100644 --- a/Libraries/LibGUI/SyntaxHighlighter.h +++ b/Libraries/LibGUI/SyntaxHighlighter.h @@ -42,8 +42,8 @@ enum class SyntaxLanguage { }; struct TextStyle { - Color color; - const Gfx::Font* font { nullptr }; + const Color color; + const bool bold { false }; }; class SyntaxHighlighter { diff --git a/Libraries/LibGUI/TextDocument.h b/Libraries/LibGUI/TextDocument.h index 8b94667187..ba78c1677f 100644 --- a/Libraries/LibGUI/TextDocument.h +++ b/Libraries/LibGUI/TextDocument.h @@ -49,7 +49,7 @@ struct TextDocumentSpan { Optional background_color; bool is_skippable { false }; bool is_underlined { false }; - const Gfx::Font* font { nullptr }; + bool bold { false }; void* data { nullptr }; }; diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp index cc233c0873..ad5fd82ea2 100644 --- a/Libraries/LibGUI/TextEditor.cpp +++ b/Libraries/LibGUI/TextEditor.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -480,7 +481,7 @@ void TextEditor::paint_event(PaintEvent& event) Gfx::IntRect character_rect = { visual_line_rect.location(), { 0, line_height() } }; for (size_t i = 0; i < visual_line_text.length(); ++i) { u32 code_point = visual_line_text.substring_view(i, 1).code_points()[0]; - const Gfx::Font* font = &this->font(); + RefPtr font = this->font(); Color color; Optional background_color; bool underline = false; @@ -490,8 +491,10 @@ void TextEditor::paint_event(PaintEvent& event) if (!span.range.contains(physical_position)) continue; color = span.color; - if (span.font) - font = span.font; + if (span.bold) { + if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700)) + font = bold_font; + } background_color = span.background_color; underline = span.is_underlined; break;