SFML community forums
Help => Graphics => Topic started by: Nemykal on October 04, 2011, 06:31:10 am
-
Hi,
What is the process for encoding a ttf font so that I can embed that data into a header file, just like arial.hpp, the default font?
Thanks
-
Convert the file to a hard-coded array of unsigned char (see Arial.hpp in SFML sources), and pass it to LoadFromMemory.
You should be able to find utilities that do this job (converting a file to a C array), although it's not very hard to write.
-
I can find a lot of information for doing this with images, but nothing with regards to .ttf files. Is there any recommended utility for encoding them?
-
Here you transform a file (a sequence of bytes) into a formatted text that represents a char array in C++ source code. It doesn't matter if the file is an image, a font or whatever. It works with any file.
#include <fstream>
#include <iomanip>
int main(int argc, char** argv)
{
if (argc <= 2)
return -1;
std::ifstream in(argv[1], std::ios::bin);
std::ofstream out(argv[2]);
out << "const char data[] = {\n";
char c;
while (in.get(c))
out << std::hex << "0x" << static_cast<int>(c) << ", ";
out << "};";
return 0;
}
I've just written this from scratch, it probably has some bugs and it's not formatted (everything's on a single line), but it should help you.
-
Been playing around with what you posted, which doesn't work by the way.
I compared the output with arial.hpp and I'm just not sure what data Font::LoadFromMemory expects. Is it expecting an array of ints, as your solution indicates, or is it expecting an array of bytes, which is what arial.hpp has? I tried a few utilities to convert files to .c source, such as Hex Workshop from http://www.bpsoft.com.
I declare the array of fontdata in a header as such:
const char terminus_data[] =
{
#include "terminus.h"
};
and terminus.h contains only the array of data, just like arial.hpp.
Compiles without issues, and LoadFromMemory returns without errors, but nothing is displayed on screen.
The only thing I changed in my initialization code was commenting out the LoadFromFile and replacing it with the LoadFromMemory, and it was working fine with LoadFromFile, so I think the problem is the format of the terminus.h
Any help is appreciated
[EDIT]
Nevermind, fixed it. I had inadvertently commented out these two lines in my drawing thread (before and after drawing the text) since I thought they weren't necessary anymore:
window->SaveGLStates();
//draw stuff
window->RestoreGLStates();
With those lines uncommented, it displays fine.
So for future reference for anyone else, I used that program I linked above in order to convert a .ttf to a c source.
-
I compared the output with arial.hpp and I'm just not sure what data Font::LoadFromMemory expects. Is it expecting an array of ints, as your solution indicates, or is it expecting an array of bytes, which is what arial.hpp has?
A byte is an integer. My code writes bytes as hexadecimal integers.
-
By int I just meant a standard int32_t
-
Ok, sorry. LoadFromMemory expects an array of char.
My code doesn't use int, see:
out << "const char data[] = {\n";
I use int only to write the bytes as formatted text, so the type doesn't really matter.