Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [solved] Embedding fonts like arial.hpp  (Read 3205 times)

0 Members and 2 Guests are viewing this topic.

Nemykal

  • Newbie
  • *
  • Posts: 6
    • View Profile
[solved] Embedding fonts like arial.hpp
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Embedding fonts like arial.hpp
« Reply #1 on: October 04, 2011, 07:42:46 am »
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.
Laurent Gomila - SFML developer

Nemykal

  • Newbie
  • *
  • Posts: 6
    • View Profile
[solved] Embedding fonts like arial.hpp
« Reply #2 on: October 04, 2011, 08:35:04 am »
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Embedding fonts like arial.hpp
« Reply #3 on: October 04, 2011, 08:43:22 am »
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.

Code: [Select]
#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.
Laurent Gomila - SFML developer

Nemykal

  • Newbie
  • *
  • Posts: 6
    • View Profile
[solved] Embedding fonts like arial.hpp
« Reply #4 on: October 04, 2011, 11:07:04 am »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Embedding fonts like arial.hpp
« Reply #5 on: October 04, 2011, 11:37:39 am »
Quote
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.
Laurent Gomila - SFML developer

Nemykal

  • Newbie
  • *
  • Posts: 6
    • View Profile
[solved] Embedding fonts like arial.hpp
« Reply #6 on: October 04, 2011, 11:48:33 am »
By int I just meant a standard int32_t

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved] Embedding fonts like arial.hpp
« Reply #7 on: October 04, 2011, 12:02:38 pm »
Ok, sorry. LoadFromMemory expects an array of char.

My code doesn't use int, see:
Code: [Select]
out << "const char data[] = {\n";
I use int only to write the bytes as formatted text, so the type doesn't really matter.
Laurent Gomila - SFML developer

 

anything