Bläddra i källkod

Be more explicit about ownership, remove unused code.

Michael Ragazzon 6 år sedan
förälder
incheckning
a613948ccd

+ 3 - 4
Include/RmlUi/Core/FontGlyph.h

@@ -29,7 +29,7 @@
 #ifndef RMLUICOREFONTGLYPH_H
 #define RMLUICOREFONTGLYPH_H
 
-#include <vector>
+#include "Types.h"
 
 namespace Rml {
 namespace Core {
@@ -45,8 +45,7 @@ class FontGlyph
 public:
 	FontGlyph() : dimensions(0,0), bearing(0,0), advance(0), bitmap_data(nullptr),
 		bitmap_dimensions(0,0)
-	{
-	}
+	{}
 
 	/// The glyph's bounding box. Not to be confused with the dimensions of the glyph's bitmap!
 	Vector2i dimensions;
@@ -59,7 +58,7 @@ public:
 
 	/// 8-bit opacity information for the glyph's bitmap. The size of the data is given by the
 	/// dimensions, below. This will be nullptr if the glyph has no bitmap data.
-	byte* bitmap_data;
+	UniquePtr<byte[]> bitmap_data;
 	/// The dimensions of the glyph's bitmap.
 	Vector2i bitmap_dimensions;
 };

+ 1 - 1
Source/Core/FontEffectOutline.cpp

@@ -99,7 +99,7 @@ bool FontEffectOutline::GetGlyphMetrics(Vector2i& origin, Vector2i& dimensions,
 // Expands the original glyph texture for the outline.
 void FontEffectOutline::GenerateGlyphTexture(byte* destination_data, const Vector2i& destination_dimensions, int destination_stride, const FontGlyph& glyph) const
 {
-	filter.Run(destination_data, destination_dimensions, destination_stride, glyph.bitmap_data, glyph.bitmap_dimensions, Vector2i(width, width));
+	filter.Run(destination_data, destination_dimensions, destination_stride, glyph.bitmap_data.get(), glyph.bitmap_dimensions, Vector2i(width, width));
 }
 
 }

+ 7 - 14
Source/Core/FontFaceHandle.cpp

@@ -44,11 +44,8 @@ FontFaceHandle::FontFaceHandle()
 
 FontFaceHandle::~FontFaceHandle()
 {
-	for (auto& pair : glyphs)
-		delete[] pair.second.bitmap_data;
-
-	for (auto& pair : layers)
-		delete pair.second;
+	glyphs.clear();
+	layers.clear();
 }
 
 // Returns the point size of this font face.
@@ -177,7 +174,7 @@ int FontFaceHandle::GenerateLayerConfiguration(const FontEffectList& font_effect
 }
 
 // Generates the texture data for a layer (for the texture database).
-bool FontFaceHandle::GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id)
+bool FontFaceHandle::GenerateLayerTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id)
 {
 	FontLayerMap::iterator layer_iterator = layers.find(layer_id);
 	if (layer_iterator == layers.end())
@@ -294,21 +291,17 @@ void FontFaceHandle::GenerateBaseLayer()
 	layer_configurations.back().push_back(base_layer);
 }
 
-Rml::Core::FontFaceLayer* FontFaceHandle::CreateNewLayer()
-{
-	return new Rml::Core::FontFaceLayer();
-}
-
 // Generates (or shares) a layer derived from a font effect.
 FontFaceLayer* FontFaceHandle::GenerateLayer(const SharedPtr<const FontEffect>& font_effect)
 {
 	// See if this effect has been instanced before, as part of a different configuration.
 	FontLayerMap::iterator i = layers.find(font_effect.get());
 	if (i != layers.end())
-		return i->second;
+		return i->second.get();
 
-	FontFaceLayer* layer = CreateNewLayer();
-	layers[font_effect.get()] = layer;
+	UniquePtr<FontFaceLayer> layer_ptr = std::make_unique<FontFaceLayer>();
+	FontFaceLayer* layer = layer_ptr.get();
+	layers[font_effect.get()] = std::move(layer_ptr);
 
 	if (!font_effect)
 	{

+ 4 - 9
Source/Core/FontFaceHandle.h

@@ -100,7 +100,7 @@ public:
 	/// @param[out] texture_dimensions The dimensions of the texture.
 	/// @param[in] layer_id The id of the layer to request the texture data from.
 	/// @param[in] texture_id The index of the texture within the layer to generate.
-	bool GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id);
+	bool GenerateLayerTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id);
 
 	/// Generates the geometry required to render a single line of text.
 	/// @param[out] geometry An array of geometries to generate the geometry into.
@@ -123,21 +123,16 @@ protected:
 	FontMetrics& GetMetrics();
 
 	void GenerateBaseLayer();
+	
+	virtual int GetKerning(CodePoint lhs, CodePoint rhs) const = 0;
 
 private:
 
-	virtual int GetKerning(CodePoint lhs, CodePoint rhs) const = 0;
-	virtual FontFaceLayer* CreateNewLayer();
-
 	FontFaceLayer* GenerateLayer(const SharedPtr<const FontEffect>& font_effect);
 
-	typedef std::vector< int > GlyphKerningList;
-	typedef std::vector< GlyphKerningList > FontKerningList;
-
 	FontGlyphMap glyphs;
-	FontKerningList kerning;
 
-	typedef SmallUnorderedMap< const FontEffect*, FontFaceLayer* > FontLayerMap;
+	typedef SmallUnorderedMap< const FontEffect*, UniquePtr<FontFaceLayer> > FontLayerMap;
 	typedef SmallUnorderedMap< size_t, FontFaceLayer* > FontLayerCache;
 	typedef std::vector< FontFaceLayer* > LayerConfiguration;
 	typedef std::vector< LayerConfiguration > LayerConfigurationList;

+ 2 - 2
Source/Core/FontFaceLayer.cpp

@@ -160,7 +160,7 @@ bool FontFaceLayer::Initialise(const FontFaceHandle* _handle, SharedPtr<const Fo
 }
 
 // Generates the texture data for a layer (for the texture database).
-bool FontFaceLayer::GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id)
+bool FontFaceLayer::GenerateTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, int texture_id)
 {
 	if (texture_id < 0 ||
 		texture_id > texture_layout.GetNumTextures())
@@ -195,7 +195,7 @@ bool FontFaceLayer::GenerateTexture(const byte*& texture_data, Vector2i& texture
 			if (glyph.bitmap_data != nullptr)
 			{
 				byte* destination = rectangle.GetTextureData();
-				byte* source = glyph.bitmap_data;
+				const byte* source = glyph.bitmap_data.get();
 
 				for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
 				{

+ 4 - 4
Source/Core/FontFaceLayer.h

@@ -43,7 +43,7 @@ class FontFaceHandle;
 
 /**
 	A textured layer stored as part of a font face handle. Each handle will have at least a base
-	layer for the standard font. Further layers can be added to allow to rendering of text effects.
+	layer for the standard font. Further layers can be added to allow rendering of text effects.
 
 	@author Peter Curry
  */
@@ -52,7 +52,7 @@ class FontFaceLayer
 {
 public:
 	FontFaceLayer();
-	virtual ~FontFaceLayer();
+	~FontFaceLayer();
 
 	/// Generates the character and texture data for the layer.
 	/// @param[in] handle The handle generating this layer.
@@ -60,14 +60,14 @@ public:
 	/// @param[in] clone The layer to optionally clone geometry and texture data from.
 	/// @param[in] deep_clone If true, the clones geometry will be completely cloned and the effect will have no option to affect even the glyph origins.
 	/// @return True if the layer was generated successfully, false if not.
-	virtual bool Initialise(const FontFaceHandle* handle, SharedPtr<const FontEffect> effect = {}, const FontFaceLayer* clone = nullptr, bool deep_clone = false);
+	bool Initialise(const FontFaceHandle* handle, SharedPtr<const FontEffect> effect = {}, const FontFaceLayer* clone = nullptr, bool deep_clone = false);
 
 	/// Generates the texture data for a layer (for the texture database).
 	/// @param[out] texture_data The pointer to be set to the generated texture data.
 	/// @param[out] texture_dimensions The dimensions of the texture.
 	/// @param[in] glyphs The glyphs required by the font face handle.
 	/// @param[in] texture_id The index of the texture within the layer to generate.
-	virtual bool GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id);
+	bool GenerateTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, int texture_id);
 	/// Generates the geometry required to render a single character.
 	/// @param[out] geometry An array of geometries this layer will write to. It must be at least as big as the number of textures in this layer.
 	/// @param[in] character_code The character to generate geometry for.

+ 11 - 7
Source/Core/FreeType/FontFaceHandle.cpp

@@ -36,7 +36,7 @@
 namespace Rml {
 namespace Core {
 
-static void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph);
+static FontGlyph BuildGlyph(FT_GlyphSlot ft_glyph);
 static void BuildGlyphMap(FT_Face ft_face, FontGlyphMap& glyphs);
 static void GenerateMetrics(FT_Face ft_face, const FontGlyphMap& glyphs, FontMetrics& metrics);
 
@@ -102,15 +102,15 @@ static void BuildGlyphMap(FT_Face ft_face, FontGlyphMap& glyphs)
 				continue;
 			}
 
-			FontGlyph glyph;
-			BuildGlyph(glyph, ft_face->glyph);
-			glyphs[(CodePoint)character_code] = glyph;
+			glyphs[(CodePoint)character_code] = BuildGlyph(ft_face->glyph);
 		}
 	}
 }
 
-static void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph)
+static FontGlyph BuildGlyph(FT_GlyphSlot ft_glyph)
 {
+	FontGlyph glyph;
+
 	// Set the glyph's dimensions.
 	glyph.dimensions.x = ft_glyph->metrics.width >> 6;
 	glyph.dimensions.y = ft_glyph->metrics.height >> 6;
@@ -138,10 +138,10 @@ static void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph)
 		}
 		else
 		{
-			glyph.bitmap_data = new byte[glyph.bitmap_dimensions.x * glyph.bitmap_dimensions.y];
+			glyph.bitmap_data.reset(new byte[glyph.bitmap_dimensions.x * glyph.bitmap_dimensions.y]);
 
 			byte* source_bitmap = ft_glyph->bitmap.buffer;
-			byte* destination_bitmap = glyph.bitmap_data;
+			byte* destination_bitmap = glyph.bitmap_data.get();
 
 			// Copy the bitmap data into the newly-allocated space on our glyph.
 			switch (ft_glyph->bitmap.pixel_mode)
@@ -189,7 +189,11 @@ static void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph)
 		}
 	}
 	else
+	{
 		glyph.bitmap_data = nullptr;
+	}
+
+	return glyph;
 }
 
 

+ 6 - 7
Source/Core/TextureLayoutTexture.cpp

@@ -36,9 +36,7 @@ namespace Rml {
 namespace Core {
 
 TextureLayoutTexture::TextureLayoutTexture() : dimensions(0, 0)
-{
-	texture_data = nullptr;
-}
+{}
 
 TextureLayoutTexture::~TextureLayoutTexture()
 {
@@ -140,21 +138,22 @@ int TextureLayoutTexture::Generate(TextureLayout& layout, int maximum_dimensions
 }
 
 // Allocates the texture.
-byte* TextureLayoutTexture::AllocateTexture()
+UniquePtr<byte[]> TextureLayoutTexture::AllocateTexture()
 {
 	// Note: this object does not free this texture data. It is freed in the font texture loader.
+	UniquePtr<byte[]> texture_data;
 
 	if (dimensions.x > 0 &&
 		dimensions.y > 0)
 	{
-		texture_data = new byte[dimensions.x * dimensions.y * 4];
+		texture_data.reset(new byte[dimensions.x * dimensions.y * 4]);
 
 		// Set the texture to transparent white.
 		for (int i = 0; i < dimensions.x * dimensions.y; i++)
-			((unsigned int*)(texture_data))[i] = 0x00ffffff;
+			((unsigned int*)(texture_data.get()))[i] = 0x00ffffff;
 
 		for (size_t i = 0; i < rows.size(); ++i)
-			rows[i].Allocate(texture_data, dimensions.x * 4);
+			rows[i].Allocate(texture_data.get(), dimensions.x * 4);
 	}
 
 	return texture_data;

+ 1 - 3
Source/Core/TextureLayoutTexture.h

@@ -64,15 +64,13 @@ public:
 
 	/// Allocates the texture.
 	/// @return The allocated texture data.
-	byte* AllocateTexture();
+	UniquePtr<byte[]> AllocateTexture();
 
 private:
 	typedef std::vector< TextureLayoutRow > RowList;
 
 	Vector2i dimensions;
 	RowList rows;
-
-	byte* texture_data;
 };
 
 }

+ 2 - 8
Source/Core/TextureResource.cpp

@@ -123,16 +123,13 @@ bool TextureResource::Load(RenderInterface* render_interface)
 	{
 		Vector2i dimensions;
 
-		bool delete_data = false;
-		const byte* data = nullptr;
+		UniquePtr<const byte[]> data = nullptr;
 
 		// Find the generation protocol and generate the data accordingly.
 		String protocol = source.substr(1, source.find("::") - 1);
 		if (protocol == "font")
 		{
 			// The requested texture is a font layer.
-			delete_data = true;
-			
 			FontFaceHandle* handle;
 			FontEffect* layer_id;
 			int texture_id;
@@ -151,10 +148,7 @@ bool TextureResource::Load(RenderInterface* render_interface)
 		if (data)
 		{
 			TextureHandle handle;
-			bool success = render_interface->GenerateTexture(handle, data, dimensions);
-
-			if (delete_data)
-				delete[] data;
+			bool success = render_interface->GenerateTexture(handle, data.get(), dimensions);
 
 			if (success)
 			{