Problem when retrieving characters in font

Discuss any general programming issues here
Post Reply
domenikov
Posts: 14
Joined: Fri May 19, 2017 10:27 pm
Location: Rome - IT

Problem when retrieving characters in font

Post by domenikov »

I'm writing a program that uses fonts with drawings and symbols instead of letters, so I've written the following code segment to test TrueType fonts, to have a way to pull all possible characters out of them:

Code: Select all

Function testFont(fileTTF)
	/* Test all chars in a font */

	Picture = CreateBrush(Nil, 2480, 3507, #WHITE)
	SelectBrush(Picture)
	OpenFont(1, fileTTF, 120, {Engine = #FONTENGINE_INBUILT})
	UseFont(1)
	SetFontColor(#BLACK)
	SetFontStyle(#NORMAL|#ANTIALIAS)

	For i=0 To 15
		For j=0 To 15
			car=i*16+j
			/* codes 0,91 and 93 gives problem */
			If car<>0 And car<>91 And car<>93
				TextOut(j*150,i*200,ConvertStr(Chr(car), #ENCODING_RAW, #ENCODING_UTF8))
			EndIf
		Next
	Next
	saveFile = "C:\\Temp\\FontTest.png"
	SaveBrush(Picture, saveFile, #WHITE, #IMGFMT_PNG)
	EndSelect()
	FreeBrush(Picture)
	CloseFont(1)
EndFunction

f$ = FileRequest("Select a TTF font")
testFont(f$)
End
This code makes the user select a TTF file and returns in a single PNG file saved on HD all the characters of it for ASCII codes from 1 to 255, in a grid.

However, it doesn't always work. It often happens that some characters are missing, and in one case they were all missing too! I also tried Chr () alone, without ENCODING_RAW and #ENCODING_UTF8 and ByteChr () instead of Chr (). All in vain, I can't get the entire character set contained in the font. This code has been executed under Hollywood 7.1.

How could I do?
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Problem when retrieving characters in font

Post by airsoftsoftwair »

What font are you testing this with?

Also note that usually TrueType fonts have many more characters than 256...
domenikov
Posts: 14
Joined: Fri May 19, 2017 10:27 pm
Location: Rome - IT

Re: Problem when retrieving characters in font

Post by domenikov »

I tested now Webdings and Wingdings II. I obtained nothing with Webdings. From Windings II, only ten characters.

I was thinking too about the fact that a TrueType font has more than 256 characters; my code was trying to take chars by using ASCII code with Chr(). In a next phase, I'll learn how to refer all characters in a font so to exploit it all.
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Problem when retrieving characters in font

Post by airsoftsoftwair »

I've examined this now. First of all, this code is wrong:

Code: Select all

TextOut(j*150,i*200,ConvertStr(Chr(car), #ENCODING_RAW, #ENCODING_UTF8))
Use this instead:

Code: Select all

TextOut(j*150,i*200,Chr(car))
This won't solve your problem, though. It will generate correct character tables for normal fonts like Arial, Courier, etc. but it won't work for Wingdings or Webdings. The problem with Wingdings and Webdings is that the TTF file doesn't contain a Unicode charmap but uses a somewhat proprietary charmap which is called MS Symbol but it isn't really consistent (otherwise it would be easily possible to map it to a Unicode charmap).

So in order to still make it possible to deal with those fonts, I've now implemented a new "Charmap" parameter for OpenFont(), SetFont(), and @FONT that allows you to specify which charmap to use. Then you'll be able to get characters from Wingdings and Webdings as well. But you won't be able to use Unicode codepoints for that. You need to use the codepoints from the MS Symbol charmaps. See here for the Wingdings codepoints: http://www.kostis.net/charsets/wingding.htm
domenikov
Posts: 14
Joined: Fri May 19, 2017 10:27 pm
Location: Rome - IT

Re: Problem when retrieving characters in font

Post by domenikov »

Thank you very much! :)
Post Reply