TTF_OpenFont -> Error writing to datastream

I have the same problem/error. Does anyone have an idea what’s wrong?

Has nobody an Idea or is it normal here, that most of the threads aren’t answered? :slight_smile:
If so, I should search for another.

Most threads are answered if someone has an answer in mind. :slight_smile:

-DanOn Thu, Jan 3, 2013 at 2:59 PM, NSpace wrote:

**
Has nobody an Idea or is it normal here, that most of the threads aren’t
answered? [image: Smile]
If so, I should search for another.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Daniel Leslie wrote:

Most threads are answered if someone has an answer in mind. :slight_smile:

-Dan

Thanks for reply. :slight_smile:

I tried to localize the error, so I tried manually that was TTF_OpenFont does and as long as I write the same Code, TTF_GetError prints the same error messages as you can see above.
To be more exact, with this Code:

Code:
SDL_RWops *rw = SDL_RWFromFile(fontFile.ptr, “rb”);
writeln(rw is null);
writeln(TTF_OpenFontIndexRW(rw, 1, 12, 0));
debug writefln(" -> Error: %s", to!(string)(TTF_GetError()));

I get:

false
null
-> Error: Error writing to datastream

So the problem is in TTF_OpenFontIndexRW.

If I change the argument for “freesrc” to 0:
Code:
writeln(TTF_OpenFontIndexRW(rw, 0, 12, 0));

I get:

false
null
-> Error: Couldn’t load font file

A different error.
But the path is correct, I validate it with:

Code:

if (!exists(fontFile)) {
throw new Exception(“Font File does not exists.”);
}

So I have no idea what’s going on. Maybe one of you.

Well, it doesn’t say “couldn’t find font file.”? It says “couldn’t load font file.”? This means that something’s going wrong at the parsing phase.? Either your font file is corrupt, or it’s in a format that SDL_TTF isn’t able to open, for whatever reason.

Mason________________________________
From: NSpace
To: sdl at lists.libsdl.org
Sent: Thursday, January 3, 2013 3:45 PM
Subject: Re: [SDL] TTF_OpenFont -> Error writing to datastream

Daniel Leslie wrote:
Most threads are answered if someone has an answer in mind.

-Dan

Thanks for reply.

I tried to localize the error, so I tried manually that was TTF_OpenFont does and as long as I write the same Code, TTF_GetError prints the same error messages as you can see above.
To be more exact, with this Code:

Code:
SDL_RWops *rw = SDL_RWFromFile(fontFile.ptr, “rb”);
writeln(rw is null);
writeln(TTF_OpenFontIndexRW(rw, 1, 12, 0));
debug writefln(" -> Error: %s", to!(string)(TTF_GetError()));
I get:

Quote:

false
null
-> Error: Error writing to datastream

So the problem is in TTF_OpenFontIndexRW.

If I change the argument for “freesrc” to 0:

Code:
writeln(TTF_OpenFontIndexRW(rw, 0, 12, 0));
I get:

Quote:
false
null
-> Error: Couldn’t load font file

A different error.
But the path is correct, I validate it with:

Code:

if (!exists(fontFile)) {
? ?throw new Exception(“Font File does not exists.”);
}

So I have no idea what’s going on. Maybe one of you.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It is a normal .ttf file (which I can also add as an attachment). Also, I have tried different ones, even the specific system fonts from Windows.

I tried this already as you can see here: http://forums.libsdl.org/viewtopic.php?p=35160#35160
Then I get the error message “Couldn’t load font file”.

I’m not only interested on solutions but it would be very helpfull if someone can explain me, why I get this error. Is anything wrong with my .ttf files? I tested 3 different .ttf files from different sources. And I validate the path, so all files are available. Or is it something wrong with my drivers? Or anything else?
It doesn’t make sense to me and is really annoying.
Thanks for your time and help.

Have you tried a different version of Freetype? The more information you
provide on what you tried, what your setup looks like, etc, the more
helpful responses you’ll get.

2013/1/4 NSpace > **

I’m not only interested on solutions but it would be very helpfull if
someone can explain me, why I get this error. Is anything wrong with my
.ttf files? I tested 3 different .ttf files from different sources. And I
validate the path, so all files are available. Or is it something wrong
with my drivers? Or anything else?
It doesn’t make sense to me and is really annoying.
Thanks for your time and help.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Gabriel.

I’m programming in D and using Derelict with FreeType version 2.4. I init SDL with INIT_VIDEO and init TTF separate with

Code:

DerelictSDL2ttf.load();

if (!TTF_WasInit()) {
	if (TTF_Init() < 0) {
		throw new Exception("TTF konnte nicht gestartet werden.");
	}
}

assert(TTF_WasInit() == 1, "SDL TTF wurde nicht korrekt initialisiert.");

and my loadFile method looks like:

Code:

void loadFromFile(string fontFile, ubyte fontSize = 0) {
	if (this._font !is null) {
		TTF_CloseFont(this._font);
	}

	if (!exists(fontFile)) {
		throw new Exception("Font File does not exists.");
	}
	
	try {
		this._font = TTF_OpenFont(fontFile.ptr, fontSize == 0 ? this._fontSize : fontSize);
		debug writefln("#1 -> Error: %s", to!(string)(TTF_GetError()));
	} catch (Throwable t) {
		debug writefln(" -> Font Size: %d", fontSize == 0 ? this._fontSize : fontSize);
		throw new Exception(.format("Error by opening font file %s: %s.", fontFile, t.msg));
	}

	if (this._font is null) {
		debug writefln("#2 -> Error: %s", to!(string)(TTF_GetError()));
		throw new Exception("Die Font konnte nicht geladen werden.");
	}

	this._fontFile = fontFile;
	this._fontSize = fontSize;
}

And as you can see this if condition throws the exception:

Code:
if (this._font is null) {
debug writefln("#2 -> Error: %s", to!(string)(TTF_GetError()));
throw new Exception(“Die Font konnte nicht geladen werden.”);
}

So TTF_Font is a null pointer.

2013/1/4 NSpace

**
I’m programming in D and using Derelict with FreeType version 2.4. I init
SDL with INIT_VIDEO and init TTF separate with

Things I would try:

  • Check file permissions (which would explain why a “file exists” check
    will succeed but a file read won’t)
  • Use a different version of Freetype (I use 2.4.6)
  • Read the font in memory and load the font via SDL_RWops from that buffer–
    Gabriel.

gabomdq wrote:

  • Read the font in memory and load the font via SDL_RWops from that buffer

And how hould I do that? An example code would be helpfull.

I tried one last attempt:

Code:

bool loadFont(string font, ubyte size, ref Texture[] textures) {
FT_Library ftLib;
int error = FT_Init_FreeType(&ftLib);

if (error != 0)
{
	writeln("dont't load lib.");
	return false; 
} 

FT_Face face; 

if ((error = FT_New_Face(ftLib, font.ptr, 0, &face)) != 0) 
{
	writeln("dont create face");
	return false; 
} 

if ((error = FT_Set_Char_Size(face, size * 64, size * 64, 0, 0)) != 0) 
{
	writeln("don't set Char size");
	return false; 
}

for (int i = 0; i < 256; i++) 
{ 
	FT_Glyph       glyph; 
	FT_BitmapGlyph bitmapGlyph; 
	FT_Bitmap      bitmap; 

	if ((error = FT_Load_Glyph(face, FT_Get_Char_Index(face, i), FT_LOAD_DEFAULT)) != 0) 
	{ 
		continue;
	} 

	if ((error = FT_Get_Glyph(face.glyph, &glyph)) != 0) 
	{ 
		continue;
	} 

	if ((error = FT_Glyph_To_Bitmap(&glyph, FT_Render_Mode.FT_RENDER_MODE_NORMAL, null, 1)) != 0) 
	{ 
		continue;
	} 

	bitmapGlyph = cast(FT_BitmapGlyph) glyph; 
	bitmap = bitmapGlyph.bitmap; 

	int width = bitmap.width; 
	int height = bitmap.rows; 

	ubyte[] data = new ubyte[2 * width * height];

	if (data is null) 
	{
		writeln("data is null");
		continue;
	} 

	for (int w = 0; w < width; w++) 
	{ 
		for (int h = 0; h < height; h++) 
		{ 

			data[2 * (w + h * width)] = data[2 * (w + h * width) + 1] = (w >= bitmap.width || 
														   h >= bitmap.rows) ? 0 : bitmap.buffer[w + bitmap.width * h]; 
		} 
	}

	textures ~= new Texture();
	
	textures[$ - 1].loadFromPixels(data, cast(ushort) width, cast(ushort) height, 32, Texture.Format.LuminanceAlpha); /*generiert eine textur aus einem GLubyte pointer*/ 

	delete data;

	//sizes[i].x = bitmap.width; //speichert die h?he/breite der einzelnen buchstaben 
	//sizes[i].y = bitmap.rows; 
}

FT_Done_Face(face); 
FT_Done_FreeType(ftLib); 

return true; 

}

This works, but if I try to render the textures I see nothing. So I create a SDL_Surface from the pixel data and saved it with SDL_SaveBMP. Then I see something, but it is strange pixel stuff. Black background and some strange mysterious coloured rectangles. But no letters.

With SDL_ttf 2.0.7 and FreeType 2.4.8 it works now. Strange. [Shocked] But I bet that no one can explain that. Am I right? :?