diff --git a/Include/RmlUi/Core/Core.h b/Include/RmlUi/Core/Core.h index 773289778..27c793d52 100644 --- a/Include/RmlUi/Core/Core.h +++ b/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 data, const String& font_family, Style::FontStyle style, Style::FontWeight weight = Style::FontWeight::Auto, bool fallback_face = false); /// Registers a generic RmlUi plugin. diff --git a/Include/RmlUi/Core/FontEngineInterface.h b/Include/RmlUi/Core/FontEngineInterface.h index 6b0acb55e..2780c6600 100644 --- a/Include/RmlUi/Core/FontEngineInterface.h +++ b/Include/RmlUi/Core/FontEngineInterface.h @@ -63,16 +63,14 @@ class RMLUICORE_API FontEngineInterface { 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 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. diff --git a/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.cpp b/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.cpp index 831be3140..23c4a51c1 100644 --- a/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.cpp +++ b/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 /*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. diff --git a/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.h b/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.h index fbc1a2589..4470e7ec4 100644 --- a/Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.h +++ b/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 @@ class FontEngineInterfaceBitmap : public Rml::FontEngineInterface { /// 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 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. diff --git a/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.cpp b/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.cpp index 4c2618d15..05092fc59 100644 --- a/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.cpp +++ b/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 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) diff --git a/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.h b/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.h index 48befdf19..df7ab4f54 100644 --- a/Samples/basic/harfbuzzshaping/src/FontEngineInterfaceHarfBuzz.h +++ b/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 @@ class FontEngineInterfaceHarfBuzz : public Rml::FontEngineInterface { 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 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 diff --git a/Samples/basic/harfbuzzshaping/src/FontProvider.cpp b/Samples/basic/harfbuzzshaping/src/FontProvider.cpp index c8d41455a..a7cc3b2be 100644 --- a/Samples/basic/harfbuzzshaping/src/FontProvider.cpp +++ b/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 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 face_memory, const String& source, String font_family, +bool FontProvider::LoadFontFace(Span data, UniquePtr face_memory, const String& source, String font_family, Style::FontStyle style, Style::FontWeight weight) { using Style::FontWeight; Vector 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 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 @@ class FontProvider { static FontProvider& Get(); - bool LoadFontFace(const byte* data, int data_size, UniquePtr face_memory, const String& source, String font_family, - Style::FontStyle style, Style::FontWeight weight); + bool LoadFontFace(Span data, UniquePtr 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 face_memory); diff --git a/Source/Core/Core.cpp b/Source/Core/Core.cpp index 7d02df9a2..5c1f14e75 100644 --- a/Source/Core/Core.cpp +++ b/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 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) diff --git a/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.cpp b/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.cpp index 2ac08bc0a..11c5d1fd9 100644 --- a/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.cpp +++ b/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 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) diff --git a/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h b/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h index 41eb6506a..a0e6fb064 100644 --- a/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h +++ b/Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h @@ -44,7 +44,7 @@ class RMLUICORE_API FontEngineInterfaceDefault : public FontEngineInterface { 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 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 diff --git a/Source/Core/FontEngineDefault/FontProvider.cpp b/Source/Core/FontEngineDefault/FontProvider.cpp index 1efb74307..bbac44872 100644 --- a/Source/Core/FontEngineDefault/FontProvider.cpp +++ b/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 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 face_memory, const String& source, - String font_family, Style::FontStyle style, Style::FontWeight weight) +bool FontProvider::LoadFontFace(Span data, bool fallback_face, UniquePtr face_memory, const String& source, String font_family, + Style::FontStyle style, Style::FontWeight weight) { using Style::FontWeight; Vector 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; diff --git a/Source/Core/FontEngineDefault/FontProvider.h b/Source/Core/FontEngineDefault/FontProvider.h index 4a8bced8c..5b206339f 100644 --- a/Source/Core/FontEngineDefault/FontProvider.h +++ b/Source/Core/FontEngineDefault/FontProvider.h @@ -63,8 +63,7 @@ class FontProvider { 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 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 @@ class FontProvider { static FontProvider& Get(); - bool LoadFontFace(const byte* data, int data_size, bool fallback_face, UniquePtr face_memory, const String& source, String font_family, + bool LoadFontFace(Span data, bool fallback_face, UniquePtr 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, diff --git a/Source/Core/FontEngineDefault/FreeTypeInterface.cpp b/Source/Core/FontEngineDefault/FreeTypeInterface.cpp index ce372f840..d55948555 100644 --- a/Source/Core/FontEngineDefault/FreeTypeInterface.cpp +++ b/Source/Core/FontEngineDefault/FreeTypeInterface.cpp @@ -78,12 +78,12 @@ void FreeType::Shutdown() } } -bool FreeType::GetFaceVariations(const byte* data, int data_length, Vector& out_face_variations) +bool FreeType::GetFaceVariations(Span data, Vector& 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(data.data()), static_cast(data.size()), 0, &face); if (error) return false; @@ -133,12 +133,14 @@ bool FreeType::GetFaceVariations(const byte* data, int data_length, Vector 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(data.data()), static_cast(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()); diff --git a/Source/Core/FontEngineDefault/FreeTypeInterface.h b/Source/Core/FontEngineDefault/FreeTypeInterface.h index 42032a7be..c4d3eaae3 100644 --- a/Source/Core/FontEngineDefault/FreeTypeInterface.h +++ b/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& out_face_variations); + bool GetFaceVariations(Span data, Vector& 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 data, const String& source, int named_instance_index = 0); // Releases the FreeType face. bool ReleaseFace(FontFaceHandleFreetype face); diff --git a/Source/Core/FontEngineInterface.cpp b/Source/Core/FontEngineInterface.cpp index 082eb2a63..e010c366b 100644 --- a/Source/Core/FontEngineInterface.cpp +++ b/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 /*data*/, const String& /*font_family*/, Style::FontStyle /*style*/, Style::FontWeight /*weight*/, bool /*fallback_face*/) { return false; diff --git a/Source/Debugger/DebuggerPlugin.cpp b/Source/Debugger/DebuggerPlugin.cpp index 72d17ebf0..7af40f67b 100644 --- a/Source/Debugger/DebuggerPlugin.cpp +++ b/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() diff --git a/changelog.md b/changelog.md index 95a215b8b..e635d7fd1 100644 --- a/changelog.md +++ b/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.