Thursday, February 14, 2008

Cyrillic letters for P+C DTR (PHP + CSS Dynamic Text Replacement) in CP1251 TTF fonts

Every time when I try to use P+C DTR script with russian (cyrillic) symbols and russian TTF fonts, I have problems with displaying it in image: russian symbols replaces with squares! But with this TTF in OpenOffice and another font viewing tools I see russian letters very good!

After may hours of googling I can't found a solution. And goes to debug script. After some hours I create a patch that resolve this problem.

String goes to ImageTTFBBox() function in UTF-8 encoding with entities (something like &u043E&u0431&u043B&u0430&0441&u0442&u044C instead "область"), but many windows TTF fonts have letters in cp1251 encoding only. And when we convert this string to windows-1251 encoding before calling function ImageTTFBBox(), PHP renders string with cp1251 TTF font correctly.

For patch P+C DTR script you need to add in heading.php after string
$text = javascript_to_html($text) ;
thouse strings:
$text = html_entity_decode ($text,ENT_NOQUOTES,'UTF-8');
$text = mb_convert_encoding($text,'cp1251','UTF-8');

And that's all, after that all 'bad' fonts displays russian symbols good!

And another patch to prevent "'Error: The server could not create this heading image." message:
My font file is located in same directory and I set path to it like this:
$font_file = 'font.ttf';
I try to use this script on different servers, it works good. But on one server with clear Debian 4.0 I see this error message instead image. Patch from excitris.com doesn't help me.
And I try to set a relative path:
$font_file = './font.ttf';
After that script in my server start works normally!

No comments: