How to use custom font(ttf)?

I got the edge version yesterday and everythign was working fine. The difference is, though, that the Win32 version requires you to specify the actual font name, whereas the android version has you specify the .ttf file.

I use it in this way:
pRet->mLabel = CCLabelTTF::labelWithString(text.c_str(), buttonSize, CCTextAlignmentCenter, “NanumMyeongjo.ttf”, 50);

This work on Win but not on Android. I think, that I specify NanumMyeongjo.ttf as file name. I checked, that ttf file is in assets
directory in apk.

Is this right way on Android?

`Issaac Ashdown
Thanks for your feedback. I reopen feature #347 now, we must keep the consistency on different platforms.

`Milda Genius
The key code is in YourGame/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java

private static Paint newPaint(String fontName, int fontSize, int alignment)
{
    // ...

    /*
    * Set type face for paint, now it support .ttf file.
    */
    if (fontName.endsWith(".ttf")){
        // trace here
    }
}

Please trace into here, and make sure the ttf type face is created successfully.

`Walzer Wang
I found my problem. I didnt update *.java files in my project from cocos2d-x edge version :(.

So after updated, I can see custom TTF font in Android build. Great!

In this time both version Win and Android works in the same way (TTF font is identify by full file name).
So I think we can close this issue.

`Issaac Ashdown
Can you please confirm this?

Hi, all,
I just checked the edge version on win32, ios & android, the Font Test works well with custom TTF.
So I closed feature #347.

Hi All

Is .otf fonts supported in cocos2dx (for android) or not.

I am getting assets not found error.

But it was working when I used the cocos2d compiled java library.

@
CCLabelBMFont* timeText = CCLabelBMFont::create(“20:00”, “BurbankBigRegular-Bold.otf”);
@

Above simple LOC is giving error.

Hi there!
this is my first post in Cocos2d-x forum,
so thank you everybody for the good work and the useful posts.

I’m using Cocos2d-x cocos2d-2.1beta3-x-2.1.0
I do my developing in Windows but my final target is iOS (iPhone/iPad).

  1. I tried to use CCLabelTTF class in Windows and then port the project to iOS to have the same look and feel,
    but I noticed that in Windows you can only use Fonts that are already installed in System/Fonts,
    not custom fonts that are included in a resources/font folder.
    Is this correct?

Regarding the use of freetype2 library which is mentioned in older posts,
I believe this will be a very good solution to have unified font rendering in all platforms,
so I have the following questions:

  1. Is the use of freetype2 library still considered for Cocos2D-x
    or have you decided to not use it?

  2. I agree that Freetype2 is a low-level library and not very easy to use it,
    but I have use it in a Qt project of mine and it’s really very fast and very stable/reliable.
    If you still considering to use Freetype2 I can offer some of my C++ source code to help performing
    the high level rendering required by CCLabelTTF.

The specs of my Freetype2 code are:

=> Font Formats Handled:
True Type(.ttf), Open Type (.otf) and Windows Type 1 (.pfb/.pfm)

=> Rendering Options:
o - Unicode: Supports rendering of any unicode character, as long as the font contains a unicode table
(all other fonts are handled as roman, only ASCII Characters 15-127 are rendered)

o - Font-Height is specified in pixels,
but we can build a conversion table/formula between Font Points and Pixel Size)

o - Free Rendering: You define the Font-Height (pixels)
Result: A bitmap of arbitrary width by rendering all supplied text in a single line.

o - Fit-To-Width Rendering: Allows to specify the Output-Image-Width (pixels) and min/max Font size (pixels).
Result: An image of ‘Output-Image-Width’ width and arbitrary height.

o - Width-Limited Multi-line Rendering: Allows to specify the Font-Height (pixels) & Output-Image-Width (pixels) and the word-wrap options (wrap on words or at any char).
Result: An image of ‘Output-Image-Width’ width and arbitrary height.

o - Align Options: Horizontally Left & Middle align is already supported.
I suppose I can easily modify the code to create a Right align and a Vertically Top/Center/Bottom which can be done on image-level.

o - Output Color Depth options: Mono (2 levels, B&W) / Gray (0-255) / Color: 3 or 4 Bytes RGB (A) with specific Font/Background colors.

The code requires some clean-up, more specifically to remove any Project-related or Qt-Specific references and convert it to standard C++,
so please tell me if you are interested to allocate the time and start working on it.