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.
This commit is contained in:
Andreas Kling 2020-12-28 15:51:43 +01:00
parent 105eb8e1bc
commit a35693915e
9 changed files with 37 additions and 34 deletions

View file

@ -51,7 +51,7 @@ void CellSyntaxHighlighter::rehighlight(Gfx::Palette palette)
Optional<Color> {},
false,
false,
nullptr,
false,
nullptr);
}

View file

@ -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<void*>(token.m_type);
spans.append(span);

View file

@ -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<void*>(token.m_type);
spans.append(span);

View file

@ -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<void*>(token.m_type);
spans.append(span);

View file

@ -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<void*>(static_cast<size_t>(type));
spans.append(span);

View file

@ -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

View file

@ -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 {

View file

@ -49,7 +49,7 @@ struct TextDocumentSpan {
Optional<Color> background_color;
bool is_skippable { false };
bool is_underlined { false };
const Gfx::Font* font { nullptr };
bool bold { false };
void* data { nullptr };
};

View file

@ -39,6 +39,7 @@
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <ctype.h>
#include <fcntl.h>
@ -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<Gfx::Font> font = this->font();
Color color;
Optional<Color> 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;