Browse Source

Use span type in `LoadFontFace` and related font engine functions

Michael Ragazzon 1 year ago
parent
commit
20d9cc7db6

+ 2 - 3
Include/RmlUi/Core/Core.h

@@ -124,8 +124,7 @@ RMLUICORE_API int GetNumContexts();
 /// @return True if the face was loaded successfully, false otherwise.
 RMLUICORE_API bool LoadFontFace(const String& file_path, bool fallback_face = false, Style::FontWeight weight = Style::FontWeight::Auto);
 /// Adds a new font face from memory to the font engine. The face's family, style and weight is given by the parameters.
-/// @param[in] data A pointer to the data.
-/// @param[in] data_size Size of the data in bytes.
+/// @param[in] data The font data.
 /// @param[in] family The family to register the font as.
 /// @param[in] style The style to register the font as.
 /// @param[in] weight The weight to load when the font face contains multiple weights, otherwise the weight to register the font as. By default it
@@ -133,7 +132,7 @@ RMLUICORE_API bool LoadFontFace(const String& file_path, bool fallback_face = fa
 /// @param[in] fallback_face True to use this font face for unknown characters in other font faces.
 /// @return True if the face was loaded successfully, false otherwise.
 /// @lifetime The pointed to 'data' must remain available until after the call to Rml::Shutdown.
-RMLUICORE_API bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style,
+RMLUICORE_API bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style,
 	Style::FontWeight weight = Style::FontWeight::Auto, bool fallback_face = false);
 
 /// Registers a generic RmlUi plugin.

+ 3 - 5
Include/RmlUi/Core/FontEngineInterface.h

@@ -63,16 +63,14 @@ public:
 	virtual bool LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight);
 
 	/// Called by RmlUi when it wants to load a font face from memory, registered using the provided family, style, and weight.
-	/// @param[in] data A pointer to the data.
-	/// @param[in] data_size Size of the data in bytes.
+	/// @param[in] data The font data.
 	/// @param[in] family The family to register the font as.
 	/// @param[in] style The style to register the font as.
 	/// @param[in] weight The weight to load when the font face contains multiple weights, otherwise the weight to register the font as.
 	/// @param[in] fallback_face True to use this font face for unknown characters in other font faces.
 	/// @return True if the face was loaded successfully, false otherwise.
-	/// Note: The debugger plugin will load its embedded font faces through this method using the family name 'rmlui-debugger-font'.
-	virtual bool LoadFontFace(const byte* data, int data_size, const String& family, Style::FontStyle style, Style::FontWeight weight,
-		bool fallback_face);
+	/// @note The debugger plugin will load its embedded font faces through this method using the family name 'rmlui-debugger-font'.
+	virtual bool LoadFontFace(Span<const byte> data, const String& family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face);
 
 	/// Called by RmlUi when a font configuration is resolved for an element. Should return a handle that
 	/// can later be used to resolve properties of the face, and generate string geometry to be rendered.

+ 2 - 2
Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.cpp

@@ -45,8 +45,8 @@ bool FontEngineInterfaceBitmap::LoadFontFace(const String& file_name, bool /*fal
 	return FontProviderBitmap::LoadFontFace(file_name);
 }
 
-bool FontEngineInterfaceBitmap::LoadFontFace(const byte* /*data*/, int /*data_size*/, const String& font_family, FontStyle /*style*/,
-	FontWeight /*weight*/, bool /*fallback_face*/)
+bool FontEngineInterfaceBitmap::LoadFontFace(Span<const byte> /*data*/, const String& font_family, FontStyle /*style*/, FontWeight /*weight*/,
+	bool /*fallback_face*/)
 {
 	// We return 'true' here to allow the debugger to continue loading, but we will use our own fonts when it asks for a handle.
 	// The debugger might look a bit off with our own fonts, but hey it works.

+ 2 - 1
Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.h

@@ -39,6 +39,7 @@ using Rml::FontFaceHandle;
 using Rml::byte;
 using Rml::Character;
 using Rml::ColourbPremultiplied;
+using Rml::Span;
 using Rml::String;
 using Rml::Texture;
 using Rml::Vector2f;
@@ -65,7 +66,7 @@ public:
 
 	/// Called by RmlUi when it wants to load a font face from memory, registered using the provided family, style, and weight.
 	/// @param[in] data A pointer to the data.
-	bool LoadFontFace(const byte* data, int data_size, const String& family, FontStyle style, FontWeight weight, bool fallback_face) override;
+	bool LoadFontFace(Span<const byte> data, const String& family, FontStyle style, FontWeight weight, bool fallback_face) override;
 
 	/// Called by RmlUi when a font configuration is resolved for an element. Should return a handle that
 	/// can later be used to resolve properties of the face, and generate string geometry to be rendered.

+ 3 - 3
Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.cpp

@@ -45,10 +45,10 @@ bool FontEngineInterfaceHarfBuzz::LoadFontFace(const String& file_name, bool /*f
 	return FontProvider::LoadFontFace(file_name, weight);
 }
 
-bool FontEngineInterfaceHarfBuzz::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style,
-	Style::FontWeight weight, bool /*fallback_face*/)
+bool FontEngineInterfaceHarfBuzz::LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
+	bool /*fallback_face*/)
 {
-	return FontProvider::LoadFontFace(data, data_size, font_family, style, weight);
+	return FontProvider::LoadFontFace(data, font_family, style, weight);
 }
 
 FontFaceHandle FontEngineInterfaceHarfBuzz::GetFontFaceHandle(const String& family, Style::FontStyle style, Style::FontWeight weight, int size)

+ 2 - 1
Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.h

@@ -40,6 +40,7 @@ using Rml::FontEffectsHandle;
 using Rml::FontFaceHandle;
 using Rml::FontMetrics;
 using Rml::RenderManager;
+using Rml::Span;
 using Rml::String;
 using Rml::TextShapingContext;
 using Rml::TexturedMeshList;
@@ -55,7 +56,7 @@ public:
 	bool LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight) override;
 
 	/// Adds a new font face to the database using the provided family, style and weight.
-	bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
+	bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
 		bool fallback_face) override;
 
 	/// Returns a handle to a font face that can be used to position and render text. This will return the closest match

+ 6 - 6
Samples/basic/harfbuzzshaping/src/FontProvider.cpp

@@ -107,27 +107,27 @@ bool FontProvider::LoadFontFace(const String& file_name, Style::FontWeight weigh
 	file_interface->Read(buffer, length, handle);
 	file_interface->Close(handle);
 
-	bool result = Get().LoadFontFace(buffer, (int)length, std::move(buffer_ptr), file_name, {}, Style::FontStyle::Normal, weight);
+	bool result = Get().LoadFontFace({buffer, length}, std::move(buffer_ptr), file_name, {}, Style::FontStyle::Normal, weight);
 
 	return result;
 }
 
-bool FontProvider::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight)
+bool FontProvider::LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight)
 {
 	const String source = "memory";
 
-	bool result = Get().LoadFontFace(data, data_size, nullptr, source, font_family, style, weight);
+	bool result = Get().LoadFontFace(data, nullptr, source, font_family, style, weight);
 
 	return result;
 }
 
-bool FontProvider::LoadFontFace(const byte* data, int data_size, UniquePtr<byte[]> face_memory, const String& source, String font_family,
+bool FontProvider::LoadFontFace(Span<const byte> data, UniquePtr<byte[]> face_memory, const String& source, String font_family,
 	Style::FontStyle style, Style::FontWeight weight)
 {
 	using Style::FontWeight;
 
 	Vector<Rml::FaceVariation> face_variations;
-	if (!Rml::FreeType::GetFaceVariations(data, data_size, face_variations))
+	if (!Rml::FreeType::GetFaceVariations(data, face_variations))
 	{
 		Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to load font face from '%s': Invalid or unsupported font face file format.", source.c_str());
 		return false;
@@ -184,7 +184,7 @@ bool FontProvider::LoadFontFace(const byte* data, int data_size, UniquePtr<byte[
 
 	for (const Rml::FaceVariation& variation : load_variations)
 	{
-		FontFaceHandleFreetype ft_face = Rml::FreeType::LoadFace(data, data_size, source, variation.named_instance_index);
+		FontFaceHandleFreetype ft_face = Rml::FreeType::LoadFace(data, source, variation.named_instance_index);
 		if (!ft_face)
 			return false;
 

+ 4 - 3
Samples/basic/harfbuzzshaping/src/FontProvider.h

@@ -34,6 +34,7 @@
 
 using Rml::byte;
 using Rml::FontFaceHandleFreetype;
+using Rml::Span;
 using Rml::String;
 using Rml::UniquePtr;
 using Rml::UnorderedMap;
@@ -69,7 +70,7 @@ public:
 	static bool LoadFontFace(const String& file_name, Style::FontWeight weight = Style::FontWeight::Auto);
 
 	/// Adds a new font face from memory.
-	static bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight);
+	static bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight);
 
 	/// Releases resources owned by sized font faces, including their textures and rendered glyphs.
 	static void ReleaseFontResources();
@@ -80,8 +81,8 @@ private:
 
 	static FontProvider& Get();
 
-	bool LoadFontFace(const byte* data, int data_size, UniquePtr<byte[]> face_memory, const String& source, String font_family,
-		Style::FontStyle style, Style::FontWeight weight);
+	bool LoadFontFace(Span<const byte> data, UniquePtr<byte[]> face_memory, const String& source, String font_family, Style::FontStyle style,
+		Style::FontWeight weight);
 
 	bool AddFace(FontFaceHandleFreetype face, const String& family, Style::FontStyle style, Style::FontWeight weight, UniquePtr<byte[]> face_memory);
 

+ 2 - 2
Source/Core/Core.cpp

@@ -326,9 +326,9 @@ bool LoadFontFace(const String& file_path, bool fallback_face, Style::FontWeight
 	return font_interface->LoadFontFace(file_path, fallback_face, weight);
 }
 
-bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
+bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
 {
-	return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
+	return font_interface->LoadFontFace(data, font_family, style, weight, fallback_face);
 }
 
 void RegisterPlugin(Plugin* plugin)

+ 3 - 3
Source/Core/FontEngineDefault/FontEngineInterfaceDefault.cpp

@@ -47,10 +47,10 @@ bool FontEngineInterfaceDefault::LoadFontFace(const String& file_name, bool fall
 	return FontProvider::LoadFontFace(file_name, fallback_face, weight);
 }
 
-bool FontEngineInterfaceDefault::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style,
-	Style::FontWeight weight, bool fallback_face)
+bool FontEngineInterfaceDefault::LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
+	bool fallback_face)
 {
-	return FontProvider::LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
+	return FontProvider::LoadFontFace(data, font_family, style, weight, fallback_face);
 }
 
 FontFaceHandle FontEngineInterfaceDefault::GetFontFaceHandle(const String& family, Style::FontStyle style, Style::FontWeight weight, int size)

+ 1 - 1
Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h

@@ -44,7 +44,7 @@ public:
 	bool LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight) override;
 
 	/// Adds a new font face to the database using the provided family, style and weight.
-	bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
+	bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
 		bool fallback_face) override;
 
 	/// Returns a handle to a font face that can be used to position and render text. This will return the closest match

+ 7 - 7
Source/Core/FontEngineDefault/FontProvider.cpp

@@ -128,28 +128,28 @@ bool FontProvider::LoadFontFace(const String& file_name, bool fallback_face, Sty
 	file_interface->Read(buffer, length, handle);
 	file_interface->Close(handle);
 
-	bool result = Get().LoadFontFace(buffer, (int)length, fallback_face, std::move(buffer_ptr), file_name, {}, Style::FontStyle::Normal, weight);
+	bool result = Get().LoadFontFace({buffer, length}, fallback_face, std::move(buffer_ptr), file_name, {}, Style::FontStyle::Normal, weight);
 
 	return result;
 }
 
-bool FontProvider::LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
+bool FontProvider::LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
 	bool fallback_face)
 {
 	const String source = "memory";
 
-	bool result = Get().LoadFontFace(data, data_size, fallback_face, nullptr, source, font_family, style, weight);
+	bool result = Get().LoadFontFace(data, fallback_face, nullptr, source, font_family, style, weight);
 
 	return result;
 }
 
-bool FontProvider::LoadFontFace(const byte* data, int data_size, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source,
-	String font_family, Style::FontStyle style, Style::FontWeight weight)
+bool FontProvider::LoadFontFace(Span<const byte> data, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source, String font_family,
+	Style::FontStyle style, Style::FontWeight weight)
 {
 	using Style::FontWeight;
 
 	Vector<FaceVariation> face_variations;
-	if (!FreeType::GetFaceVariations(data, data_size, face_variations))
+	if (!FreeType::GetFaceVariations(data, face_variations))
 	{
 		Log::Message(Log::LT_ERROR, "Failed to load font face from '%s': Invalid or unsupported font face file format.", source.c_str());
 		return false;
@@ -205,7 +205,7 @@ bool FontProvider::LoadFontFace(const byte* data, int data_size, bool fallback_f
 
 	for (const FaceVariation& variation : load_variations)
 	{
-		FontFaceHandleFreetype ft_face = FreeType::LoadFace(data, data_size, source, variation.named_instance_index);
+		FontFaceHandleFreetype ft_face = FreeType::LoadFace(data, source, variation.named_instance_index);
 		if (!ft_face)
 			return false;
 

+ 2 - 3
Source/Core/FontEngineDefault/FontProvider.h

@@ -63,8 +63,7 @@ public:
 	static bool LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight = Style::FontWeight::Auto);
 
 	/// Adds a new font face from memory.
-	static bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight,
-		bool fallback_face);
+	static bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face);
 
 	/// Return the number of fallback font faces.
 	static int CountFallbackFontFaces();
@@ -81,7 +80,7 @@ private:
 
 	static FontProvider& Get();
 
-	bool LoadFontFace(const byte* data, int data_size, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source, String font_family,
+	bool LoadFontFace(Span<const byte> data, bool fallback_face, UniquePtr<byte[]> face_memory, const String& source, String font_family,
 		Style::FontStyle style, Style::FontWeight weight);
 
 	bool AddFace(FontFaceHandleFreetype face, const String& family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face,

+ 6 - 4
Source/Core/FontEngineDefault/FreeTypeInterface.cpp

@@ -78,12 +78,12 @@ void FreeType::Shutdown()
 	}
 }
 
-bool FreeType::GetFaceVariations(const byte* data, int data_length, Vector<FaceVariation>& out_face_variations)
+bool FreeType::GetFaceVariations(Span<const byte> data, Vector<FaceVariation>& out_face_variations)
 {
 	RMLUI_ASSERT(ft_library);
 
 	FT_Face face = nullptr;
-	FT_Error error = FT_New_Memory_Face(ft_library, (const FT_Byte*)data, data_length, 0, &face);
+	FT_Error error = FT_New_Memory_Face(ft_library, static_cast<const FT_Byte*>(data.data()), static_cast<FT_Long>(data.size()), 0, &face);
 	if (error)
 		return false;
 
@@ -133,12 +133,14 @@ bool FreeType::GetFaceVariations(const byte* data, int data_length, Vector<FaceV
 	return true;
 }
 
-FontFaceHandleFreetype FreeType::LoadFace(const byte* data, int data_length, const String& source, int named_style_index)
+FontFaceHandleFreetype FreeType::LoadFace(Span<const byte> data, const String& source, int named_style_index)
 {
 	RMLUI_ASSERT(ft_library);
 
 	FT_Face face = nullptr;
-	FT_Error error = FT_New_Memory_Face(ft_library, (const FT_Byte*)data, data_length, (named_style_index << 16), &face);
+	FT_Error error =
+		FT_New_Memory_Face(ft_library, static_cast<const FT_Byte*>(data.data()), static_cast<FT_Long>(data.size()), (named_style_index << 16), &face);
+
 	if (error)
 	{
 		Log::Message(Log::LT_ERROR, "FreeType error %d while loading face from %s.", error, source.c_str());

+ 2 - 2
Source/Core/FontEngineDefault/FreeTypeInterface.h

@@ -42,10 +42,10 @@ namespace FreeType {
 	void Shutdown();
 
 	// Returns a sorted list of available font variations for the font face located in memory.
-	bool GetFaceVariations(const byte* data, int data_length, Vector<FaceVariation>& out_face_variations);
+	bool GetFaceVariations(Span<const byte> data, Vector<FaceVariation>& out_face_variations);
 
 	// Loads a FreeType face from memory, 'source' is only used for logging.
-	FontFaceHandleFreetype LoadFace(const byte* data, int data_length, const String& source, int named_instance_index = 0);
+	FontFaceHandleFreetype LoadFace(Span<const byte> data, const String& source, int named_instance_index = 0);
 
 	// Releases the FreeType face.
 	bool ReleaseFace(FontFaceHandleFreetype face);

+ 1 - 1
Source/Core/FontEngineInterface.cpp

@@ -43,7 +43,7 @@ bool FontEngineInterface::LoadFontFace(const String& /*file_path*/, bool /*fallb
 	return false;
 }
 
-bool FontEngineInterface::LoadFontFace(const byte* /*data*/, int /*data_size*/, const String& /*font_family*/, Style::FontStyle /*style*/,
+bool FontEngineInterface::LoadFontFace(Span<const byte> /*data*/, const String& /*font_family*/, Style::FontStyle /*style*/,
 	Style::FontWeight /*weight*/, bool /*fallback_face*/)
 {
 	return false;

+ 3 - 4
Source/Debugger/DebuggerPlugin.cpp

@@ -258,10 +258,9 @@ bool DebuggerPlugin::LoadFont()
 {
 	const String font_family_name = "rmlui-debugger-font";
 
-	return (LoadFontFace(courier_prime_code, sizeof(courier_prime_code) / sizeof(courier_prime_code[0]), font_family_name, Style::FontStyle::Normal,
-				Style::FontWeight::Normal) &&
-		LoadFontFace(courier_prime_code_italic, sizeof(courier_prime_code_italic) / sizeof(courier_prime_code_italic[0]), font_family_name,
-			Style::FontStyle::Italic, Style::FontWeight::Normal));
+	return (LoadFontFace({courier_prime_code, sizeof(courier_prime_code)}, font_family_name, Style::FontStyle::Normal, Style::FontWeight::Normal) &&
+		LoadFontFace({courier_prime_code_italic, sizeof(courier_prime_code_italic)}, font_family_name, Style::FontStyle::Italic,
+			Style::FontWeight::Normal));
 }
 
 bool DebuggerPlugin::LoadMenuElement()

+ 4 - 0
changelog.md

@@ -245,6 +245,10 @@ input { nav: auto; nav-right: #ok_button; }
   - `Box::Edge` -> `BoxEdge` (e.g. `Box::TOP` -> `BoxEdge::Top`, values now in titlecase).
   - `Box::Direction` -> `BoxDirection` (e.g. `Box::VERTICAL` -> `BoxDirection::Vertical`, values now in titlecase).
   - `Property::Unit` -> `Unit` (e.g. `Property::PX` -> `Unit::PX`).
+
+#### Core functions
+
+- Changed the signature of `LoadFontFace` (from memory) to take a `Span` instead of a raw pointer. 
 - Replaced `Element::ResolveNumericProperty` with `Element::ResolveLength` and `Element::ResolveNumericValue`. Can be used together with `Property::GetNumericValue`.
 - Renamed and removed several `Math` functions.