Hello! I am trying to render an emoji on screen using Google's Noto font. However, it always either renders blank squares, or nothing at all. Here's a link to the download:
https://fonts.google.com/noto/specimen/Noto+Color+Emoji (https://fonts.google.com/noto/specimen/Noto+Color+Emoji)
And here's the small amount of code I've written to try and display it.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "Emoji", sf::Style::Close);
sf::Event event;
sf::Clock clock;
while (window.isOpen())
{
while(window.pollEvent(event))
{
switch (event.type)
{
case(sf::Event::Closed):
{
window.close();
}
}
}
window.clear();
sf::Font font;
font.loadFromFile("NotoColorEmoji-Regular.ttf");
sf::Text text;
text.setFont(font);
text.setString("Test Text");
window.draw(text);
window.display();
}
return 0;
}
What am I doing wrong? I'm pretty sure this font doesn't contain any standard characters, which makes sense, but if that's the case, how do I render anything at all? Do I use Unicode? Glyphs? If it's either, then how do I use them?
I'm VERY new to the concept of fonts and character encoding as a whole, so I understand if I've missed something completely obvious.
Thank you for your help!
It doesn't look like sf::String (https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1String.php) takes a UTF-8 string as a parameter. It seems to presume any chains of 8-bit characters (e.g. an std::string) is an "ANSI string". This is not Unicode so you cannot pass Unicode to sf::String with UTF-8 in its constructor.
Maybe try UTF-16 (using a wide string) or UTF-32 (string of sf::Uint32).
That said, you can create an sf::String with fromUtf8 (https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1String.php#aa7beb7ae5b26e63dcbbfa390e27a9e4b) and then assign it to your actual string. e.g.:
std::string uStr{ u8"🌙" };
sf::String str{ sf::String::fromUtf8(uStr.begin(), uStr.end()) };
I'd expect that to work but I haven't tested it :P I'm 100% on if it'll accept those iterators.