tags:

views:

752

answers:

3

Hi,

I'm trying to render images with verdana text using PHP imagettftext function. However, all east asian characters are not being rendered correctly. I tried using other fonts like tahoma and Lucida Grande, but neither works. Arial Unicode, however, works perfectly.

The problem is that I don't want to use Arial as my font. Is there any way I could render using a font like Verdana?

(not a unicode and/or font expert; so explanation of the problem would be helpful)

+1  A: 

IIRC your truetype font must have unicode characters within it.

You can see how many characters a certain font has in the Character Map tool in windows.

Ólafur Waage
+3  A: 

None of Verdana, Tahoma or Lucida Grande contain Chinese/Japanese/Korean characters so they will never be able to render them “correctly”. No font contains glyphs for every Unicode code point, so whichever you pick you're going to end up with missing glyphs at some point.

There is a font that is like Verdana and contains some CJK characters — specifically Japanese kanji. Maybe that's enough to cover what you're trying to do? It's called Meiryo and comes with Windows Vista/7. You can also get it here. However it is a TrueType Collection (.ttc) file, which I'm not sure if PHP/GD can handle directly; many East Asian fonts come as .ttc files, because they typically contain both proportional and monospace fonts based on the same glyph shapes.

If PHP can't cope with .ttc, you might need to convert the Meiryo-proportional font out of it into a separate .ttf file, using either a font editor, or the simple ‘breakttc.exe’ command-line tool from the TrueType SDK (ttsdk.zip, mysteriously vanished from microsoft.com, but easily locatable using Google).

bobince
A: 

I've rendered Japanese characters with imagettftext before, by using the East Asian font directly. There are a few which come packaged with Windows : "simsun" (Chinese) "MS Gothic" (Japanese) "Ms Mincho" (Japanese) The last two are the equivalent of sans serif and serif for Japanese text. I think the Chinese ones have both styles too. In my old php code it uses font files with a ".ttc" extension, not sure why. Perhaps TTF is the definition and the actual font is a TTC file? (that was long ago)

Anyway, as for using Arial etc, on Windows at least the way it works is that the system detectes characters outside of the "western" Unicode range, and will automatically switch to one of the default installed "Asian" fonts to render those. Thise cause among other things, some users to get chinese characters instead of Japanese or vice versa, because many of those characters share the same unicode range, but depending on which font is used, you egt the "chinese" stlye or the japanese style.

Mixing both fonts doesn't look great. Using purely a Japanese font will render the alphabetic characters in a "monospaced" font, which doesn't look too good.

If all else fail, you could try to split your string into separate western/eastern character range substrings (loop on characters and test unicode range), and then render each part with the appropriate font. Keep in mind the Eastern fonts are monospaced (at least chinese/japanese, I don't know about korean, etc).

faB