Am I must install ttf to make custom font label on windows?

I am developing on windows.

Recently, I spend much time to use custom font label.

I fixed engine code that reference with http://www.cocos2d-x.org/forums/6/topics/32189, http://www.cocos2d-x.org/forums/6/topics/14665.

But it wasn’t fixed.

Today, I found IF install that ttf, then I can use that font to make label.

But I think that it’s so strange… isn’t it?

And it looks like when I create CCLabelTTF, it needs FONT NAME not font file name. Is it right?

Hi,
I’m a noob. But digging in the code for Win32. But yes there seems to be a problem there to display fonts that are not already installed on windows

Looking at the windows specific code for BitmapDC there seems to be a few bugs in the code.

I did find a workaround that requires no code chages: Call setFontName on the label like this

   `CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "fonts/Felt.ttf", 24);`
   `pLabel->setFontName("fonts/Felt.ttf");`

Note
The name of the ttf file MUST match the font face inside. So Mark_Felt.ttf that is provided by the examples can’t be loaded and must be renamed to Felt.ttf the name of the font

The reason this happens:

  1. The code to add the Font Resource from file is incorrectly placed in a block that is only called when removing a previously loaded font: see BitmapDC::setFont in platform/win32/CCImage.cpp:160

  2. The font-face to load in the call CreateFontIndirectA is generated from the file name (taking the file name without the extension) however TTF files contain the font-face name in the definition. That means that if the font-face is not the same as the file name the operation will fail.
    An example it the Marker Felt.ttf provided as an example. The font-face in the TTf file is Felt so it will not load correctly without renaming.

Additional Issues:

  1. Windows errors are not reported. For instance if AddFontResource fails to load the ttf file the error is not reported anywhere

  2. The windows documentation says that the WM_FONTCHANGE should be sent with HWND_BROADCAST to inform all windows of the new font however the SendMessage is called with to a single specific window

  3. The current design has additional Fonts generated for every Label instead of loaded once to the application and then reused.

Finally if you fixed the engine code according to the thread above then your problem is most likely due to a font_file_name / font_face_name mismatch.

open the ttf font with Windows Font Viewer and make sure that the Name of the file matches exactly the ‘Font name’ that appears in the first line

Thank you, Oded Sharon. I want to fix that and contribute to cocos2dx with commiting, but I have too many works to do. TT

I added a pull request for v3. It works for me now if I make sure that the ttf files are named correctly

Coool!

Oded Sharon wrote:

I added a pull request for v3. It works for me now if I make sure that the ttf files are named correctly