0

I'm currently hardcoding my images into my C++ program as a struct (the source code with the pixel data can be created with GIMP), like so:

static const struct {
  unsigned int   width;
  unsigned int   height;
  unsigned int   bytes_per_pixel;
  unsigned char  pixel_data[16 * 16 * 4 + 1];
} my_icon = {
  16, 16, 4,
  "...", pixel data here
};

This is working fine and I'm very happy! But now I want to do the same for the font file I'm using.

Since GIMP did the conversion for me but obviously can't convert the font for me, I'm stuck now and don't know how I would go about hardcoding the font.ttf like I do with the images.

If it is of any relevance, this is how I currently load the font file:

sf::Font font;
if (!font.loadFromFile("font.ttf")) {
    std::cout << "Could not load font" << std::endl;
    return -1;
}

Could someone please help me? (The font is less than 25 KB in size so no problem there)

Thanks!!

Joelle Denby
  • 13
  • 1
  • 3

2 Answers2

1

It seem Font has a fine loadFromMemory method. So you can use it exactly as your previous example, presenting the data in a string literal and pass it over.

It's not hard to write a program that converts the binary file in \xHH hexdump-like format suitable for inclusion.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
  • Could you please tell me what format this is: "\307R\377\357\305W\377\3..." I have tried a binary to Hex converter and a hexdump tool like this: http://www.fileformat.info/tool/hexdump.htm but it's not in the above format. I don't know how it's called. I have Googled like crazy to convert the font to this format but I can't find anything :( – Joelle Denby Jul 01 '13 at 14:37
  • It looks as though that's a constant pointer to the sequence of bytes {0xC7,0x52,0xFF,0xEF,0xC5,0x57,0xFF,...}. A backslash followed by three digits represents that byte value in *octal*; a printable character that isn't a backshash, ending quote, or question mark represents its character code in the destination character set (typically ASCII). – supercat Jul 01 '13 at 22:28
  • The command line tool xxd converts binary files to hex arrays in C format with `xxd -i `. – Vortico Jul 02 '13 at 13:26
  • Thanks guys. I Googled a bit and found the tool Bin2h for Windows. It's working perfectly! – Joelle Denby Jul 04 '13 at 17:13
1

All right, this is my solution:

I used the tool Bin2h (http://www.deadnode.org/sw/bin2h/) to create the following code:

unsigned int fontSize = 22256;
unsigned char fontChar[] = {
0x00,0x01,0x00,...
};

Then I use

font.loadFromMemory(fontChar, fontSize)

to load the font.

Joelle Denby
  • 13
  • 1
  • 3