|
ChartDirector and UTF8 |
Posted by Elisha on Mar-29-2014 02:10 |
|
Hi there,
I'm trying to display names that are UTF8 encoded on a graph in Perl. These names are pulled from a database and encoded to utf8 using Perl's encode_utf8 function. At the moment, I'm just trying to get this to work for Chinese names. After reading the documentation and some of the previous questions on this forum, here's what I did:
1. Download mingliu.ttc
2. libchartdir.so is located in /usr/local/lib, so I copied mingliu.ttc to /usr/local/lib/fonts
3. In the code, wrote $chart->xAxis()->setLabelStyle("mingliu.ttc", 28, 0x777777);
What comes out is garbled characters.
Any help is greatly appreciated, thanks! |
Re: ChartDirector and UTF8 |
Posted by Peter Kwan on Mar-29-2014 04:20 |
|
Hi Elisha,
If you see garbled characters, most likely the encoding is not correct. If you see empty
characters or "square boxes" instead of characters, the encoding is correct, and most
likely the font is not correct.
To test if the font is correct or not, please try the following code:
$title = "\\N{U+65E5}\\N{U+6708}";
$title = encode_utf8($title);
$c->addTitle($title, "mingliu.ttc");
The code above should display two Chinese characters meaning the "SUN" and the
"MOON" in the chart title. If you can see the two characters, you can confirm that your
system can show Chinese characters in charts. The issue in this case is most likely due
to encoding.
The encode_utf8 method is to change from "Perl's internal encoding" to UTF8. When you
read the data from your database into Perl, are you sure the text is in "Perl's internal
encoding"? (It depends on your database schema, and how the database driver is set
up.) If it is in "Perl's internal encoding", then encode_utf8 should work. If it is not in
"Perl's internal encoding", Perl does not understand what is the text, and so it is unable
to convert the text to UTF8 using encode_utf8.
One simple method to check if Perl can understand the text is to check the length. An
example is:
$myFirstLabel = $$labels[0]; //assume the first axis label contains Chinese
$lengthOfLabel = length($myFirstLabel);
$myFirstLabel = encode_utf8($myFirstLabel);
$c->addTitle("Length = $lengthOfLabel " . $myFirstLabel, "mingliu.ttc");
If the label contains 5 chinese characters, but Perl thinks the text contains 10
characters, obviously Perl cannot understand the text, and in this case, encode_utf8 will
produce 10 garbled characters.
In many case, the text may not be in "Perl's internal encoding". For example, the text
from the database may be in "Big5" encoding, which is not "Perl's internal encoding". To
convert it to UTF8, the Text::iconv should be used. See:
http://search.cpan.org/~mpiotr/Text-Iconv-1.7/Iconv.pm
It is also possible the text from the database is already in UTF8 encoding, but Perl does
not know it is in UTF8 encoding. If this is the case, no conversion is necessary.
If the above still does not solve the problem, is it possible to attach an example chart
with the chart title produced using the $myFirstLabel code above?
Regards
Peter Kwan |
Re: ChartDirector and UTF8 |
Posted by Elisha on Mar-31-2014 22:00 |
|
Thanks a lot for your reply!
I did your first step, which was to set the title. I actually saw nothing in the title of the chart. I also tested with $title = "test"; just to make sure I had the code right. Would this indicate a font issue then? |
Re: ChartDirector and UTF8 |
Posted by Peter Kwan on Mar-31-2014 23:52 |
|
Hi Elisha,
Font problem is possible.
Attached please find a test script. Please try to run it. It should create a chart image
"test.png" as attached. I have attached the "test.png" I got for your reference. It should
also print the result of the font loading test for "mingliu.ttc". The font loading test can
inform you which directories ChartDirector has searched for "mingliu.ttc", and why it cannot
load the file.
Please kindly let me know what is the result.
Regards
Peter Kwan
|
Re: ChartDirector and UTF8 |
Posted by Elisha on Apr-01-2014 00:14 |
|
Hi Peter,
I get the following errors:
Searching for mingliu.ttc - No such file or directory [err code = 2]
Error loading /usr/local/lib/fonts/mingliu.ttc [FT err code = 2]
Does FT err code = 2 mean it still doesn't find mingliu.ttc in /usr/local/lib/fonts? Doing an ls on the directory shows that it's there with the same permissions and groups as the default fonts that come with ChartDirector for Linux.
I attached the test.png that I got out, but I didn't expect the Chinese characters to load correctly given the errors.
Thanks!
|
Re: ChartDirector and UTF8 |
Posted by Peter Kwan on Apr-01-2014 00:50 |
|
Hi Elisha,
I have just checked. The error "FT err code = 2" means "unknown file format". I means the
font engine is unable to determine what type of file is "mingliu.ttc".
To diagnose the problem, please try to copy the "mingliu.ttc" from Windows to your host
once more. If you are using FTP, please make sure the FTP is in binary mode (as opposed to
ASCII mode). If the above still cannot solve the problem, is it possible to gzip the file
directly on the Linux host (use gzip mingliu.ttc from the command line) and email to me. My
email is pkwan@advsofteng.net. Note that the gzip (or zip) it needs to be done directly on
the Linux host so we can get a copy of the exact file on the host. If the file is FTP or
copied to Windows before zipping, then it may be changed during the copying.
Regards
Peter Kwan |
Re: ChartDirector and UTF8 |
Posted by Elisha on Apr-01-2014 00:58 |
|
Hi Peter,
Unfortunately, I actually do not have a Windows machine. I obtained the mingliu font from this location:
http://www.fontsupply.com/fonts/M/Mingliu.html
The download contains mingliu.zip from which I extracted mingliu.ttc and placed in the /usr/local/lib/fonts directory. |
Re: ChartDirector and UTF8 |
Posted by Peter Kwan on Apr-01-2014 01:37 |
|
Hi Elisha,
I have just downloaded and tried the font your mentioned using Windows. I just tried to
open the font on Windows (not using ChartDirector, but just use the Windows OS to open
the font), and it produces error. So I confirm it is not a valid font.
If you do not have Windows, you can use any OS you have that can use True Type fonts
to test this font. It should produce error as well. (If you are using a desktop computer to
access this forum, your computing probably supports GUI, and your computer should be able
to open fonts to confirm if it is valid or not.)
Regards
Peter Kwan |
Re: ChartDirector and UTF8 |
Posted by Elisha on Apr-01-2014 02:28 |
|
Hi Peter,
Thanks for clearing that part up! I did think it was weird that I couldn't open the font when I downloaded it, but didn't think to attribute it to my problem because I was having the same problems with other fonts that did open. Turns out, my problems with the other fonts were permissions issues (which I missed the first time and then re-discovered using the testFont function). Everything works as expected now.
Thank you so much!
Elisha |
|