cour.ttf works but not arial.ttf and string Garbage at end

Hey I've implemented a writeText function.

1.
First problem is that if i load arial.ttf or many other fonts the program crashes.
However cour.ttf works fine and has both big and small capped letters.

2.
second problem is that when i use the sprintf() function to fill the buffer that should be displayed on screen I get garbage at the end of the string that is passed to the LAST occurence of writeText().

I tried end it with \0 terminator but it still puts some garbage at the end like so:
"format string %d\0"

The only thing that DOES work is to end the string with TWO spaces like so:
"format string %d "




writeText() function :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void writeText(stbtt_bakedchar *BCD, GameBuffer *gameBuffer, int *index, float x, float y, char *text)
{

	static int advance = 0;
	while(*text)
	{
		
		float PX = 0;
		float PY = 0;
		stbtt_aligned_quad q = {};

		int character = *text - 32;
		stbtt_GetBakedQuad(BCD, SCR_WIDTH, 200, character, &PX, &PY, &q, 1); 

		if(*text == (int)' ')
		{
			q.x1 = 10.5f;
		}

		if(*index > TEXT_LAST)
		{
			MessageBox(0, "Need to increase number of characters (TEXT_LAST)", "MAX TEXT INDEX REACHED", MB_OK);
			*text = 0;
		}

		gameBuffer[*index].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f); 
		gameBuffer[*index].createObject(x + advance, y, q.x1, -q.y0);
		gameBuffer[*index].setTextureCoordinates(q.s0, q.t0, q.s1, q.t1);
		gameBuffer[*index].setSolidColor(1.0f, 1.0f, 1.0f, 1.0);

		advance += q.x1;
		*index += 1;
		text++;
	}

	advance = 0;
}



main.cpp (parts of main.cpp) :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int main()
{

       // setting up font info and stuff with stb_truetype

	stbtt_bakedchar BCD[96] = {};
	unsigned char fontBuffer[200000] = {};
	const float fontMapWidth  = SCR_WIDTH;
	const float fontMapHeight = 200;
	unsigned char fontData[SCR_WIDTH*SCR_HEIGHT] = {};



// loading arial.ttf here crashes the program

	FILE *hFontFile = fopen("C:\\windows\\fonts\\cour.ttf", "rb");
	if(!hFontFile)
	{
		printf("fail open file\n");
	}

	fread(fontBuffer, 1, sizeof(fontBuffer), hFontFile);
	stbtt_BakeFontBitmap(fontBuffer, 0, 24.0f, fontData, fontMapWidth, fontMapHeight, 32, 126, BCD);


// ... ... ...
// ... ... ...
// ... ... ...



	char DEBUGMouseCurPos[220] = {};
	char DEBUGFramTime[220] = {};
	char DEBUGWindowDim[220] = {}


while(running)
{


          //  .... calling the function in the while loop


		sprintf(DEBUGWindowDim, "Window Dimensions: %d/%d", winWidth, winHeight);
		sprintf(DEBUGMouseCurPos, "MouseCursor: %d/%d", (int)mouseCurPosX, (int)mouseCurPosY);
		sprintf(DEBUGFramTime, "miliSecs/frame: %0.2f  ", time.getMiliSeconds());

		int currTxtIndex = TEXT_FIRST; 
		writeText(BCD, gameBuffer, &currTxtIndex, textX, 300.0f, DEBUGWindowDim); 
		writeText(BCD, gameBuffer, &currTxtIndex, textX, 350.0f, DEBUGMouseCurPos); 
		writeText(BCD, gameBuffer, &currTxtIndex, textX, 550.0f, DEBUGFramTime); 

// last call to writeText() prints some garbage at the end of the string.



// ... some mapping and drawing code
// ... ... ...
}

Edited by C_Worm on Reason: Initial post
(1) You read only 200000 bytes for ttf font file. On my system aria.ttf file is 1036584 bytes, cour.ttf is 806504 bytes. So stb probably would crash if you'll pass only part of ttf font file to it. Or read some garbage data after end of array.

(2) Maybe this is an issue - your BCD is too short. You are passing to stbtt_BakeFontBitmap function 126 as count of characters. But you have space allocated only for 96.

But from what I can tell rest of code is fine. Bug with garbage at end of string is either related to problem in (1) or (2) or it is somewhere else in code.

Edited by Mārtiņš Možeiko on
Boooom! that was it, thaanks so much! <3