Results 1 to 12 of 12

Thread: Loading TTF fonts from resources in 3.0

  1. #1
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default Loading TTF fonts from resources in 3.0

    I'm not seeing a way to load TTF fonts from resources. Prior to 3.0, I had fonts in resources, but I'm not seeing a way to do that in 3.0. I tried putting the resource ID in the font description name, and then it attempted to load the TTF, but it couldn't find the file referenced in my "ttf" element.

    What's the right way to load a ttf font from resources?

    I need a solution for this asap, any help is greatly appreciated!

    EDIT: After reading the code, it looks to me like Font_Impl::load_font doesn't try to load from resources at all.... In previous versions, I was able to pull an IODevice from the resources and construct a CL_Font_Freetype and pass that IODevice in to the constructor, which let me load TTF fonts from resources... I need to be able to do this in 3.0 as well. Help!
    Last edited by Nightwind0; 11-02-2013 at 05:40 PM.

  2. #2
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Did you look at http://clanlib.org/documentation/3.0/FontResource.html "Using the <ttf> element"

    For example (from looking at the docs)

    Code:
    <font name="mysuperfont">
      <ttf file="resources/mysuperfont.ttf"/>
    </font>
    And use:

    Code:
    static Resource<Font> clan::Font::resource (Canvas & canvas, const FontDescription & desc, const ResourceManager & resources )
    
    Retrieves a Font resource from the resource manager.
    eg
    clan::FontDescription desc;
    desc.set_typeface_name("mysuperfont");
    desc.set_height(24);
    clan::Font mysuperfont = clan::Font::resource(canvas, desc, resources);

  3. #3
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Quote Originally Posted by rombust View Post
    Did you look at http://clanlib.org/documentation/3.0/FontResource.html "Using the <ttf> element"

    For example (from looking at the docs)

    Code:
    <font name="mysuperfont">
      <ttf file="resources/mysuperfont.ttf"/>
    </font>
    And use:

    Code:
    static Resource<Font> clan::Font::resource (Canvas & canvas, const FontDescription & desc, const ResourceManager & resources )
    
    Retrieves a Font resource from the resource manager.
    eg
    clan::FontDescription desc;
    desc.set_typeface_name("mysuperfont");
    desc.set_height(24);
    clan::Font mysuperfont = clan::Font::resource(canvas, desc, resources);
    Yeah, this is what I tried. It doesnt find the file and throws an exception.


    EDIT: I managed to get it to work but I had to provide the full path to the ttf file. In other words, I can't simply refer to it inside my resources the way I can other resources. Is this a bug, or a restriction of version 3.0?
    Last edited by Nightwind0; 11-03-2013 at 04:39 AM.

  4. #4
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    I had a quick look at the source, it looks like a bug:

    In Sources/Display/font.cpp

    Font Font_Impl::load(Canvas &canvas, const FontDescription &reference_desc, const std::string &id, const XMLResourceDocument &doc, Callback_2<Resource<Sprite>, Canvas &, const std::string &> cb_get_sprite)

    Change...
    Code:
    if (ttf_element.has_attribute("file"))
    	filename = ttf_element.get_attribute("file"); desc.set_typeface_name(ttf_element.get_attribute("file"));
    To...

    Code:
    if (ttf_element.has_attribute("file"))
    {
    	filename = PathHelp::combine(resource.get_base_path(), ttf_element.get_attribute("file"));
    	desc.set_typeface_name(ttf_element.get_attribute("file"));
    }
    If that works, we will apply the patch.

  5. #5
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Quote Originally Posted by rombust View Post
    I had a quick look at the source, it looks like a bug:

    In Sources/Display/font.cpp

    Font Font_Impl::load(Canvas &canvas, const FontDescription &reference_desc, const std::string &id, const XMLResourceDocument &doc, Callback_2<Resource<Sprite>, Canvas &, const std::string &> cb_get_sprite)

    Change...
    Code:
    if (ttf_element.has_attribute("file"))
    	filename = ttf_element.get_attribute("file"); desc.set_typeface_name(ttf_element.get_attribute("file"));
    To...

    Code:
    if (ttf_element.has_attribute("file"))
    {
    	filename = PathHelp::combine(resource.get_base_path(), ttf_element.get_attribute("file"));
    	desc.set_typeface_name(ttf_element.get_attribute("file"));
    }
    If that works, we will apply the patch.
    Yay, that works, thank you!! By the way, I also had to include "path_help.h" in font.cpp when I made that change.

  6. #6
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Thanks again for the patch! It's working for me in my Linux build (I haven't tried windows yet)


    By the way, I was looking at the Font Engine for win32 code, like this:
    Code:
    AddFontResourceEx(StringHelp::utf8_to_ucs2(filename).c_str(), FR_PRIVATE|FR_NOT_ENUM, 0);
    and I'm wondering if this will work in windows in a zipped resource file?

  7. #7
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Quote Originally Posted by Nightwind0 View Post
    By the way, I was looking at the Font Engine for win32 code, like this:
    Code:
    AddFontResourceEx(StringHelp::utf8_to_ucs2(filename).c_str(), FR_PRIVATE|FR_NOT_ENUM, 0);
    and I'm wondering if this will work in windows in a zipped resource file?
    No, it will not work. Although it should be easy to fix.

    Instead of usng AddFontResourceEx, use AddFontMemResourceEx

    Loading the font into a clan::DataBuffer,
    Code:
    	DataBuffer data(file.get_size());
    	file.read(data.get_data(), data.get_size());
    	int out_number_of_fonts = 0;
    	HANDLE handle = AddFontMemResourceEx(data.get_data(), data.get_size(), 0, &out_number_of_fonts);
    Imho, you should not need to call: RemoveFontMemResourceEx(handle); since it's unloaded automatically when the application exits. I'm unsure about repeated calls to AddFontMemResourceEx using the same font, but, it "should" be okay and "ignore" the subsequent calls


    ( http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx )

  8. #8
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Quote Originally Posted by rombust View Post
    No, it will not work. Although it should be easy to fix.

    Instead of usng AddFontResourceEx, use AddFontMemResourceEx

    Loading the font into a clan:ataBuffer,
    Code:
    	DataBuffer data(file.get_size());
    	file.read(data.get_data(), data.get_size());
    	int out_number_of_fonts = 0;
    	HANDLE handle = AddFontMemResourceEx(data.get_data(), data.get_size(), 0, &out_number_of_fonts);
    Imho, you should not need to call: RemoveFontMemResourceEx(handle); since it's unloaded automatically when the application exits. I'm unsure about repeated calls to AddFontMemResourceEx using the same font, but, it "should" be okay and "ignore" the subsequent calls


    ( http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx )

    Oh, cool. Are you going to patch the Win32 font engine with that?

  9. #9
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Exclamation

    So, I thought your earlier patch fixed my problem, but it only fixes it in certain situations (when your working directory is the same as where resources are). I see the real problem, which is that when we load most resources, the FileSystem object gets passed around so that the file can be loaded from it. However, in the case of TTF Fonts, we don't do that, we only pass a filename.
    Is there a way we can do that, and implement the fonts using the memory based Win32 font API and the Freetype for Linux? I really need to be able to package my TTF fonts into a zip, and I currently can't do that.












    Quote Originally Posted by rombust View Post
    No, it will not work. Although it should be easy to fix.

    Instead of usng AddFontResourceEx, use AddFontMemResourceEx

    Loading the font into a clan:ataBuffer,
    Code:
    	DataBuffer data(file.get_size());
    	file.read(data.get_data(), data.get_size());
    	int out_number_of_fonts = 0;
    	HANDLE handle = AddFontMemResourceEx(data.get_data(), data.get_size(), 0, &out_number_of_fonts);
    Imho, you should not need to call: RemoveFontMemResourceEx(handle); since it's unloaded automatically when the application exits. I'm unsure about repeated calls to AddFontMemResourceEx using the same font, but, it "should" be okay and "ignore" the subsequent calls


    ( http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx )

  10. #10
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    I can't see a reason not to do that. The API would need to change, but I don't see that's a problem.

    I'm not sure how hard it would be to implement, in theory it should be easy.

  11. #11
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Okay, I've gone ahead and done that. I'm including a zip file with the files I changed.

    I had to add one constructor to Font. I tested my patch under Linux and it works, and I made sure that it compiles in Win32 but I haven't actually tried the Win32 version (which uses AddFontMemResourceEx), however, I based it on the code you posted earlier in this thread, and it should work.



    Quote Originally Posted by rombust View Post
    I can't see a reason not to do that. The API would need to change, but I don't see that's a problem.

    I'm not sure how hard it would be to implement, in theory it should be easy.
    Attached Files Attached Files

  12. #12
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Patch applied. Many thanks

Similar Threads

  1. resources.xml and backgrounds
    By Funzo in forum Official ClanLib SDK Forums
    Replies: 6
    Last Post: 03-05-2012, 08:41 AM
  2. Working with resources
    By Ockonal in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 12-18-2010, 09:17 PM
  3. loading sprite fonts from css
    By xrubio in forum Official ClanLib SDK Forums
    Replies: 13
    Last Post: 07-31-2009, 08:35 AM
  4. RPG tiles resources?
    By whisperstorm in forum Novashell Game Creation System
    Replies: 2
    Last Post: 02-15-2007, 02:28 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •