Search code examples
c++stringfreetype

How do I iterate a std::string to get a set of Freetype FT_ULong?


std::string is commonly interpreted as UTF8, hence has a variable length encoding. In my font renderer I've hit a problem in that I'm not sure how to get a "character" from a std::string and convert it into a Freetype FT_ULong in order to get a glyph with FT_Get_Char_Index. That is to say, I am not sure that what I'm doing is "correct" as I'm just iterating through std::string and casting the resulting chars over (surely this is incorrect, although it works with my OS defaults).

So is there a "correct" way of doing this and more importantly has someone written a library that implements this "correct" way that I can use off the shelf?


Solution

  • You should first check how UTF8 is encoded, and would know that what kind of start bits are with how many bytes.

    See http://en.wikipedia.org/wiki/UTF8

    And then you can write code like this:

      if ((byte & 0x80) == 0x00) {
        // 1 byte UTF8 char
      }
      else if ((byte & 0xE0) == 0xC0) {
        // 2 bytes UTF8 char
      }
      else if ...
    

    Then you can iterates each UTF8 characters in the std::string with correct bytes.