Browse Source

Layout engine: Avoid text wrapping due to float imprecision by rounding up available width.

Michael Ragazzon 4 years ago
parent
commit
8bf0f6af2e
3 changed files with 19 additions and 1 deletions
  1. 8 0
      Include/RmlUi/Core/Math.h
  2. 1 1
      Source/Core/LayoutLineBox.cpp
  3. 10 0
      Source/Core/Math.cpp

+ 8 - 0
Include/RmlUi/Core/Math.h

@@ -149,6 +149,14 @@ RMLUICORE_API float RoundFloat(float value);
 /// @param[in] value The value to round.
 /// @param[in] value The value to round.
 /// @return The rounded integer as double.
 /// @return The rounded integer as double.
 RMLUICORE_API double RoundFloat(double value);
 RMLUICORE_API double RoundFloat(double value);
+/// Rounds a floating-point value up to the nearest integer.
+/// @param[in] value The value to round.
+/// @return The rounded integer as float.
+RMLUICORE_API float RoundUpFloat(float value);
+/// Rounds a floating-point value down to the nearest integer.
+/// @param[in] value The value to round.
+/// @return The rounded integer as float.
+RMLUICORE_API float RoundDownFloat(float value);
 /// Rounds a floating-point value to the nearest integer.
 /// Rounds a floating-point value to the nearest integer.
 /// @param[in] value The value to round.
 /// @param[in] value The value to round.
 /// @return The rounded integer.
 /// @return The rounded integer.

+ 1 - 1
Source/Core/LayoutLineBox.cpp

@@ -301,7 +301,7 @@ LayoutInlineBox* LayoutLineBox::AddBox(UniquePtr<LayoutInlineBox> box_ptr)
 
 
 	float available_width = -1;
 	float available_width = -1;
 	if (wrap_content)
 	if (wrap_content)
-		available_width = dimensions.x - (open_inline_box->GetPosition().x + open_inline_box->GetBox().GetPosition(Box::CONTENT).x);
+		available_width = Math::RoundUpFloat(dimensions.x - (open_inline_box->GetPosition().x + open_inline_box->GetBox().GetPosition(Box::CONTENT).x));
 
 
 	// Flow the box's content into the line.
 	// Flow the box's content into the line.
 	UniquePtr<LayoutInlineBox> overflow_box = open_inline_box->FlowContent(first_box, available_width, right_spacing);
 	UniquePtr<LayoutInlineBox> overflow_box = open_inline_box->FlowContent(first_box, available_width, right_spacing);

+ 10 - 0
Source/Core/Math.cpp

@@ -137,6 +137,16 @@ RMLUICORE_API double RoundFloat(double value)
 	return round(value);
 	return round(value);
 }
 }
 
 
+RMLUICORE_API float RoundUpFloat(float value)
+{
+	return ceilf(value);
+}
+
+RMLUICORE_API float RoundDownFloat(float value)
+{
+	return floorf(value);
+}
+
 // Rounds a floating-point value to the nearest integer.
 // Rounds a floating-point value to the nearest integer.
 RMLUICORE_API int RoundToInteger(float value)
 RMLUICORE_API int RoundToInteger(float value)
 {
 {