Browse Source

Reduce size of ElementText

Michael Ragazzon 3 years ago
parent
commit
3b9ca3f686
2 changed files with 40 additions and 35 deletions
  1. 10 8
      Include/RmlUi/Core/ElementText.h
  2. 30 27
      Source/Core/ElementText.cpp

+ 10 - 8
Include/RmlUi/Core/ElementText.h

@@ -111,16 +111,20 @@ private:
 	using LineList = Vector< Line >;
 	LineList lines;
 
-	bool dirty_layout_on_change;
-
 	GeometryList geometry;
-	bool geometry_dirty;
+
+	// The decoration geometry we've generated for this string.
+	UniquePtr<Geometry> decoration;
 
 	Colourb colour;
 	float opacity;
 
-	// The decoration geometry we've generated for this string.
-	Geometry decoration;
+	int font_handle_version;
+
+	bool geometry_dirty : 1;
+
+	bool dirty_layout_on_change : 1;
+	
 	// What the decoration type is that we have generated.
 	Style::TextDecoration generated_decoration;
 	// What the element's actual text-decoration property is; this may be different from the generated decoration
@@ -128,10 +132,8 @@ private:
 	// it isn't being changed.
 	Style::TextDecoration decoration_property;
 
-	FontEffectsHandle font_effects_handle;
 	bool font_effects_dirty;
-
-	int font_handle_version;
+	FontEffectsHandle font_effects_handle;
 };
 
 } // namespace Rml

+ 30 - 27
Source/Core/ElementText.cpp

@@ -44,23 +44,13 @@ namespace Rml {
 static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space, bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters);
 static bool LastToken(const char* token_begin, const char* string_end, bool collapse_white_space, bool break_at_endline);
 
-ElementText::ElementText(const String& tag) : Element(tag), colour(255, 255, 255), opacity(1), decoration(this)
-{
-	dirty_layout_on_change = true;
-
-	generated_decoration = Style::TextDecoration::None;
-	decoration_property = Style::TextDecoration::None;
-
-	geometry_dirty = true;
+ElementText::ElementText(const String& tag) :
+	Element(tag), colour(255, 255, 255), opacity(1), font_handle_version(0), geometry_dirty(true), dirty_layout_on_change(true),
+	generated_decoration(Style::TextDecoration::None), decoration_property(Style::TextDecoration::None), font_effects_dirty(true),
+	font_effects_handle(0)
+{}
 
-	font_effects_handle = 0;
-	font_effects_dirty = true;
-	font_handle_version = 0;
-}
-
-ElementText::~ElementText()
-{
-}
+ElementText::~ElementText() {}
 
 void ElementText::SetText(const String& _text)
 {
@@ -105,10 +95,19 @@ void ElementText::OnRender()
 	// Regenerate text decoration if necessary.
 	if (decoration_property != generated_decoration)
 	{
-		decoration.Release(true);
+		if (decoration_property == Style::TextDecoration::None)
+		{
+			decoration.reset();
+		}
+		else
+		{
+			if (decoration)
+				decoration->Release(true);
+			else
+				decoration = MakeUnique<Geometry>(this);
 
-		if (decoration_property != Style::TextDecoration::None)
 			GenerateDecoration(font_face_handle);
+		}
 
 		generated_decoration = decoration_property;
 	}
@@ -153,8 +152,8 @@ void ElementText::OnRender()
 			geometry[i].Render(translation);
 	}
 
-	if (decoration_property != Style::TextDecoration::None)
-		decoration.Render(translation);
+	if (decoration)
+		decoration->Render(translation);
 }
 
 // Generates a token of text from this element, returning only the width.
@@ -316,7 +315,6 @@ void ElementText::ClearLines()
 
 	lines.clear();
 	generated_decoration = Style::TextDecoration::None;
-	decoration.Release(true);
 }
 
 // Adds a new line into the text element.
@@ -397,6 +395,8 @@ void ElementText::OnPropertyChange(const PropertyIdSet& changed_properties)
 	if (changed_properties.Contains(PropertyId::TextDecoration))
 	{
 		decoration_property = computed.text_decoration();
+		if (decoration && decoration_property == Style::TextDecoration::None)
+			decoration.reset();
 	}
 
 	if (font_face_changed)
@@ -411,11 +411,14 @@ void ElementText::OnPropertyChange(const PropertyIdSet& changed_properties)
 		geometry_dirty = true;
 
 		// Re-colour the decoration geometry.
-		Vector< Vertex >& vertices = decoration.GetVertices();
-		for (size_t i = 0; i < vertices.size(); ++i)
-			vertices[i].colour = colour;
+		if (decoration)
+		{
+			Vector<Vertex>& vertices = decoration->GetVertices();
+			for (size_t i = 0; i < vertices.size(); ++i)
+				vertices[i].colour = colour;
 
-		decoration.Release();
+			decoration->Release();
+		}
 	}
 }
 
@@ -471,7 +474,6 @@ void ElementText::GenerateGeometry(const FontFaceHandle font_face_handle)
 	for (size_t i = 0; i < lines.size(); ++i)
 		GenerateGeometry(font_face_handle, lines[i]);
 
-	decoration.Release(true);
 	generated_decoration = Style::TextDecoration::None;
 
 	geometry_dirty = false;
@@ -488,9 +490,10 @@ void ElementText::GenerateGeometry(const FontFaceHandle font_face_handle, Line&
 void ElementText::GenerateDecoration(const FontFaceHandle font_face_handle)
 {
 	RMLUI_ZoneScopedC(0xA52A2A);
+	RMLUI_ASSERT(decoration);
 	
 	for(const Line& line : lines)
-		GeometryUtilities::GenerateLine(font_face_handle, &decoration, line.position, line.width, decoration_property, colour);
+		GeometryUtilities::GenerateLine(font_face_handle, decoration.get(), line.position, line.width, decoration_property, colour);
 }
 
 static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space, bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters)