Skip to content

Commit

Permalink
Use span type in LoadFontFace and related font engine functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Mar 31, 2024
1 parent 386820b commit 20d9cc7
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 51 deletions.
5 changes: 2 additions & 3 deletions Include/RmlUi/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,15 @@ 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
/// loads all found font weights.
/// @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.
Expand Down
8 changes: 3 additions & 5 deletions Include/RmlUi/Core/FontEngineInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
Expand Down
4 changes: 2 additions & 2 deletions Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Samples/basic/bitmapfont/src/FontEngineInterfaceBitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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
Expand Down
12 changes: 6 additions & 6 deletions Samples/basic/harfbuzzshaping/src/FontProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions Samples/basic/harfbuzzshaping/src/FontProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

using Rml::byte;
using Rml::FontFaceHandleFreetype;
using Rml::Span;
using Rml::String;
using Rml::UniquePtr;
using Rml::UnorderedMap;
Expand Down Expand Up @@ -69,7 +70,7 @@ class FontProvider {
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();
Expand All @@ -80,8 +81,8 @@ class FontProvider {

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);

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/FontEngineDefault/FontEngineInterfaceDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/FontEngineDefault/FontEngineInterfaceDefault.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand Down
14 changes: 7 additions & 7 deletions Source/Core/FontEngineDefault/FontProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions Source/Core/FontEngineDefault/FontProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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();
Expand All @@ -81,7 +80,7 @@ class FontProvider {

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,
Expand Down
10 changes: 6 additions & 4 deletions Source/Core/FontEngineDefault/FreeTypeInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/FontEngineDefault/FreeTypeInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/FontEngineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions Source/Debugger/DebuggerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 20d9cc7

Please sign in to comment.