Skip to content

Commit d2cc1a4

Browse files
authored
Add LoadCodepoints bindings (#428)
* Add LoadCodepoints bindings * Add codepoint docs, tests and exports
1 parent 161665c commit d2cc1a4

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎include/Font.hpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ class Font : public FontUnmanaged {
6363
FontUnmanaged::Load(fileName, fontSize, codepoints, codepointCount);
6464
}
6565

66+
/**
67+
* Loads a font from a given file, with the given codepoints.
68+
*
69+
* @param fileName The file path of the font to load.
70+
* @param fontSize The desired size of the loaded font.
71+
* @param codepoints The codepoints to generate glyphs for.
72+
*
73+
* @throws raylib::RaylibException Throws if the given font failed to initialize.
74+
*/
75+
void Load(const std::string& fileName, int fontSize, const std::vector<int>& codepoints) {
76+
Unload();
77+
FontUnmanaged::Load(fileName, fontSize, codepoints);
78+
}
79+
6680
void Load(const ::Image& image, ::Color key, int firstChar) {
6781
Unload();
6882
FontUnmanaged::Load(image, key, firstChar);

‎include/FontUnmanaged.hpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RAYLIB_CPP_INCLUDE_FONTUNMANAGED_HPP_
33

44
#include <string>
5+
#include <vector>
56

67
#include "./RaylibException.hpp"
78
#include "./TextureUnmanaged.hpp"
@@ -55,6 +56,19 @@ class FontUnmanaged : public ::Font {
5556
Load(fileName, fontSize, codepoints, codepointCount);
5657
}
5758

59+
/**
60+
* Loads a Font from the given file, with the given codepoints.
61+
*
62+
* @param fileName The file path of the font to load.
63+
* @param fontSize The desired size of the loaded font.
64+
* @param codepoints The codepoints to generate glyphs for.
65+
*
66+
* @throws raylib::RaylibException Throws if the given font failed to initialize.
67+
*/
68+
FontUnmanaged(const std::string& fileName, int fontSize, const std::vector<int>& codepoints) {
69+
Load(fileName, fontSize, codepoints);
70+
}
71+
5872
/**
5973
* Loads a Font from the given image with a color key.
6074
*
@@ -123,6 +137,19 @@ class FontUnmanaged : public ::Font {
123137
}
124138
}
125139

140+
/**
141+
* Loads a font from a given file, with the given codepoints.
142+
*
143+
* @param fileName The file path of the font to load.
144+
* @param fontSize The desired size of the loaded font.
145+
* @param codepoints The codepoints to generate glyphs for.
146+
*
147+
* @throws raylib::RaylibException Throws if the given font failed to initialize.
148+
*/
149+
void Load(const std::string& fileName, int fontSize, const std::vector<int>& codepoints) {
150+
Load(fileName, fontSize, codepoints.data(), static_cast<int>(codepoints.size()));
151+
}
152+
126153
/**
127154
* Loads a font from an image with a color key.
128155
*

‎include/Functions.hpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,35 @@ RLCPP_MAYBEUNUSED RLCPPAPI inline std::string TextToCamel(const std::string& tex
445445
return ::TextToCamel(text.c_str());
446446
}
447447

448+
/**
449+
* Load all codepoints from a UTF-8 text string
450+
*
451+
* @param text The UTF-8 encoded text to load the codepoints from.
452+
*
453+
* @return The codepoints that were found in the given text.
454+
*/
455+
RLCPP_MAYBEUNUSED RLCPPAPI std::vector<int> LoadCodepoints(const std::string& text) {
456+
int count = 0;
457+
int* codepoints = ::LoadCodepoints(text.c_str(), &count);
458+
if (codepoints != NULL) {
459+
std::vector<int> output(codepoints, codepoints + count);
460+
::UnloadCodepoints(codepoints);
461+
return output;
462+
}
463+
return std::vector<int>();
464+
}
465+
466+
/**
467+
* Get total number of codepoints in a UTF-8 encoded string
468+
*
469+
* @param text The UTF-8 encoded text to count the codepoints of.
470+
*
471+
* @return The total number of codepoints in the given text.
472+
*/
473+
RLCPP_MAYBEUNUSED RLCPPAPI inline int GetCodepointCount(const std::string& text) {
474+
return ::GetCodepointCount(text.c_str());
475+
}
476+
448477
/**
449478
* Get integer value from text (negative values not supported)
450479
*/

‎modules/raylib.cppm‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ using raylib::GetDirectoryPath;
132132
using raylib::GetPrevDirectoryPath;
133133
using raylib::GetWorkingDirectory;
134134
using raylib::LoadDirectoryFiles;
135+
using raylib::LoadDirectoryFilesEx;
135136
using raylib::ChangeDirectory;
136137
using raylib::LoadDroppedFiles;
137138
using raylib::GetFileModTime;
@@ -158,7 +159,12 @@ using raylib::TextFindIndex;
158159
using raylib::TextToUpper;
159160
using raylib::TextToLower;
160161
using raylib::TextToPascal;
162+
using raylib::TextToSnake;
163+
using raylib::TextToCamel;
164+
using raylib::LoadCodepoints;
165+
using raylib::GetCodepointCount;
161166
using raylib::TextToInteger;
167+
using raylib::TextToFloat;
162168

163169
/**
164170
* @namespace raylib::Colors

‎tests/raylib_cpp_test.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,33 @@ int main(int argc, char* argv[]) {
125125
AssertEqual(output[1], "How");
126126
}
127127

128+
// raylib::GetCodepointCount() and raylib::LoadCodepoints()
129+
{
130+
// ASCII text has one codepoint per byte.
131+
std::string ascii = "Hello";
132+
AssertEqual(raylib::GetCodepointCount(ascii), 5);
133+
134+
std::vector<int> asciiCodepoints = raylib::LoadCodepoints(ascii);
135+
AssertEqual(static_cast<int>(asciiCodepoints.size()), raylib::GetCodepointCount(ascii));
136+
AssertEqual(asciiCodepoints[0], static_cast<int>('H'));
137+
AssertEqual(asciiCodepoints[4], static_cast<int>('o'));
138+
139+
// Multi-byte UTF-8 text holds fewer codepoints than bytes: "a", U+00E9 and U+20AC.
140+
std::string utf8 = "a\xC3\xA9\xE2\x82\xAC";
141+
AssertEqual(static_cast<int>(utf8.size()), 6);
142+
AssertEqual(raylib::GetCodepointCount(utf8), 3);
143+
144+
std::vector<int> utf8Codepoints = raylib::LoadCodepoints(utf8);
145+
AssertEqual(static_cast<int>(utf8Codepoints.size()), raylib::GetCodepointCount(utf8));
146+
AssertEqual(utf8Codepoints[0], 0x61);
147+
AssertEqual(utf8Codepoints[1], 0xE9);
148+
AssertEqual(utf8Codepoints[2], 0x20AC);
149+
150+
// An empty string holds no codepoints.
151+
AssertEqual(raylib::GetCodepointCount(""), 0);
152+
AssertEqual(static_cast<int>(raylib::LoadCodepoints("").size()), 0);
153+
}
154+
128155
// Wave
129156
{
130157
raylib::Wave wave(path + "/resources/weird.wav");

0 commit comments

Comments
 (0)