Skip to content

Commit

Permalink
Text widget: Use rounded line height to make render output more stabl…
Browse files Browse the repository at this point in the history
…e when scrolling vertically
  • Loading branch information
mikke89 committed Jul 7, 2024
1 parent 65301ad commit 283e842
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Source/Core/Elements/ElementFormControlTextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void ElementFormControlTextArea::SetCompositionRange(int range_start, int range_
bool ElementFormControlTextArea::GetIntrinsicDimensions(Vector2f& dimensions, float& /*ratio*/)
{
dimensions.x = (float)(GetNumColumns() * ElementUtilities::GetStringWidth(this, "m"));
dimensions.y = (float)GetNumRows() * GetLineHeight();
dimensions.y = (float)GetNumRows() * Math::Round(GetLineHeight());

return true;
}
Expand Down
18 changes: 11 additions & 7 deletions Source/Core/Elements/WidgetTextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,10 @@ void WidgetTextInput::ProcessEvent(Event& event)
case Input::KI_END: selection_changed = MoveCursorHorizontal(ctrl ? CursorMovement::End : CursorMovement::EndLine, shift, out_of_bounds); break;

case Input::KI_NUMPAD9: if (numlock) break; //-fallthrough
case Input::KI_PRIOR: selection_changed = MoveCursorVertical(-int(internal_dimensions.y / parent->GetLineHeight()) + 1, shift, out_of_bounds); break;
case Input::KI_PRIOR: selection_changed = MoveCursorVertical(-int(internal_dimensions.y / GetLineHeight()) + 1, shift, out_of_bounds); break;

case Input::KI_NUMPAD3: if (numlock) break; //-fallthrough
case Input::KI_NEXT: selection_changed = MoveCursorVertical(int(internal_dimensions.y / parent->GetLineHeight()) - 1, shift, out_of_bounds); break;
case Input::KI_NEXT: selection_changed = MoveCursorVertical(int(internal_dimensions.y / GetLineHeight()) - 1, shift, out_of_bounds); break;

case Input::KI_BACK:
{
Expand Down Expand Up @@ -1056,8 +1056,7 @@ void WidgetTextInput::SetCursorFromRelativeIndices(int cursor_line_index, int cu

int WidgetTextInput::CalculateLineIndex(float position) const
{
float line_height = parent->GetLineHeight();
int line_index = int(position / line_height);
int line_index = int(position / GetLineHeight());
return Math::Clamp(line_index, 0, (int)(lines.size() - 1));
}

Expand Down Expand Up @@ -1239,7 +1238,7 @@ Vector2f WidgetTextInput::FormatText(float height_constraint)
selected_text_element->ClearLines();

// Determine the line-height of the text element.
const float line_height = parent->GetLineHeight();
const float line_height = GetLineHeight();

const float half_leading = 0.5f * (line_height - (font_metrics.ascent + font_metrics.descent));
const float top_to_baseline = font_metrics.ascent + half_leading;
Expand Down Expand Up @@ -1415,7 +1414,7 @@ Vector2f WidgetTextInput::FormatText(float height_constraint)
void WidgetTextInput::GenerateCursor()
{
cursor_size.x = Math::Round(ElementUtilities::GetDensityIndependentPixelRatio(text_element));
cursor_size.y = text_element->GetLineHeight() + 2.0f;
cursor_size.y = GetLineHeight() + 2.0f;

Colourb color = parent->GetComputedValues().color();

Expand Down Expand Up @@ -1450,7 +1449,7 @@ void WidgetTextInput::UpdateCursorPosition(bool update_ideal_cursor_position)

cursor_position = {
(float)string_width_pre_cursor + alignment_offset,
-1.f + (float)cursor_line_index * text_element->GetLineHeight(),
-1.f + (float)cursor_line_index * GetLineHeight(),
};

const bool word_wrap = parent->GetComputedValues().white_space() == Style::WhiteSpace::Prewrap;
Expand Down Expand Up @@ -1584,6 +1583,11 @@ void WidgetTextInput::SetKeyboardActive(bool active)
}
}

float WidgetTextInput::GetLineHeight() const
{
return Math::Round(parent->GetLineHeight());
}

float WidgetTextInput::GetAvailableWidth() const
{
return parent->GetClientWidth() - parent->GetBox().GetFrameSize(BoxArea::Padding).x;
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Elements/WidgetTextInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class WidgetTextInput : public EventListener {
/// Returns the offset that aligns the contents of the line according to the 'text-align' property.
float GetAlignmentSpecificTextOffset(const Line& line) const;

/// Returns the used line height.
float GetLineHeight() const;
/// Returns the width available for the text contents without overflowing, that is, the content area subtracted by any scrollbar.
float GetAvailableWidth() const;
/// Returns the height available for the text contents without overflowing, that is, the content area subtracted by any scrollbar.
Expand Down

0 comments on commit 283e842

Please sign in to comment.