Hi All,
I got one small problem. I want to set the title in russian language for example. $plot->SetXTitle($lang['Date & Time']); I get funny characters instead.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Two things are needed to make this work: (1) you must be using a TrueType text font which contains the characters you need, and (2) you have to encode your characters correctly in the PHP string you pass to SetXTitle(). (PHPlot doesn't do anything with the characters in your string - it just sends the string to the GD library function.)
So first make sure you are using a TrueType font, using SetDefaultTTFont(), SetUseTTF(), or SetFontTTF(). You want to be using a Unicode font that has the Russian (Cyrillic) characters you need, but that should not be hard. For example, both DejaVu and Liberation open-source TTF fonts should work.
The harder part is going to be the way your text is encoded or escaped. XML decimal numeric encoded sequences work, for example: "€" for a Euro symbol. Also UTF-8 encoded multibyte escapes in strings work, for example "\xe2\x82\xac" also for a Euro. As far as I know, those are the only two ways to encode characters in PHP that work with GD, but I don't know a lot about it.
What exactly do you have in your $lang array?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I want to add that yes, you can have a string in PHP with the actual UTF-8 characters, and they will be plotted correctly. I hadn't tried this before (I have trouble getting those characters in and displaying them in my text editor), but I just tried it and it works. It is in fact documented behavior for the PHP GD function imagettftext(). So if you have a text editor that can handle UTF-8 entry and display, you can enter your characters that way right into PHP string values and they should plot correctly.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear Ibayuk,
Let me thank you for your effort. Ok what we are doing is a translation template. So I have php which keep the translation of each word say vehicle in english so is call $lang['Vehicle'] will get the translated term which is keep in the individual language .php for instance russian.php etc. I am using ultraedit to write my php. First the encode was ascii (dos) then I covert it to UTF-8. I could use this method to work for all my other pages. Comes to phplot it does not work. For you information if I convert ascii to unicode all my others goes wrong gets some funny chinese characters rather. I hope this helps.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, I understood that $lang[] was for localization (translation strings), but was wondering if you had UTF-8 strings values in there (vs something else, like decimal encoded entities). With UTF-8 encoded strings in there, it should work.
What about the font? Did you set PHPlot to use a TrueType font for the title(s), and does that font have the characters you need?
If it isn't the font, it could be something in the array isn't what we think, and that is going to be harder to debug.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can you try the attached file chartest.php? This doesn't use PHPlot, just the underlying PHP/GD routines. You can run it with the PHP CLI (redirect output to file.png), or put it on a web server and access it through the browser. The output is a PNG file with some test strings. (You will need to edit the script to change the font path to something you have; make sure your editor doesn't re-encode the file.)
Dear Ibayuk,
For you information the ultra edit file which stored localisation is for sure it UTF-8 because else all of it appear as group of question marks ???????. Ok I tried this now $plot->SetDefaultTTFont('phPlot/arial.ttf'); and yes it works. So must I put the arial.ttf file to be there always. What does this do ? Is there anything I must do ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Great progress, so it looks like a font issue. Yes you will need to make PHPlot use a TrueType font (one which contains the Unicode groups you need) for its text. This is because PHP/GD's built-in fonts are not UTF-8 encoded, and have few non-ASCII characters.
Depending on your server or hosting environment, TrueType fonts should aready be available. You just need to pick one and tell PHPlot to use it with one of the 3 functions I listed.
There are also free fonts you can include with your application if you want. (Arial isn't free, so technically you can't copy it.) This includes the Liberation and DejaVu font families, but there are many others.
Dear Ibayuk,
MY server is Centos 6.4. So what do you think I pick from server the arial font? For test I just took the .ttf file from my windows. So what is your best advice here.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
CentOS has TrueType fonts available, however I don't know which ones are installed by default or on your server. You can check by looking in /usr/share/fonts (which is where Red Hat and CentOS keep fonts). Inside there will be directories for font families.
On CentOS, /usr/share/fonts/liberation contains the Liberation font family, and /usr/share/fonts/dejavu contains the DejaVu family. If you have them. Both of these are good, and both contain at least some Cyrillic characters. The DejaVu family is bigger (contains many more Unicode characters). The Liberation family is meant as a drop-in replacement for Microsoft's fonts Arial, Times New Roman, and Courier New. You can use whichever you prefer, or any other TrueType font that works for you.
For maximum chance of success, although non-portable to other systems, you can specify a full font file name in PHPlot like this:
Dear Ibayuk,
Yes I went into /usr/share/fonts/liberation and found a list of it. So is dejavu is better cause I got that too in my system. So I am very new to this but confuse with this TrueType ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, I don't know any Russian so I have no idea what these characters mean and which ones are commonly used or not. If you are a Russian speaker, or have someone doing translations for you, then it is possible to look at the characters available in the font to make a choice. Your server or PC should have an application to look at fonts. On my Linux PCs it is under the menu Accessories > Character Map (program name is "gucharmap"). Select the font name from the pull-down, and be sure to check the box View > Show only glyphs from this font, if you want to see what is available.
But since you have the more complete DejaVu font family, why not just go with that?
That font should support any language you need. It is also available in bold and italic, should you want to use different fonts for titles or labels.
TrueType is a kind of font file. TrueType fonts are usually very high quality (compared to the simple fixed-cell fonts that are built in to PHP/GD). On the other hand, TrueType text is slower to render (draw), so this might be a concern if you have very critical performance issues. Also you might find TrueType font text to be less readable at very small point sizes. So if your plots have small labels with only numbers (where Unicode characters are not an issue), you might choose to use the built-in GD fonts for the labels, and use TrueType for the bigger titles.
Does this help, or confuse things even more?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear Ibayuk,
I think this is helpful enough ready $plot->SetDefaultTTFont('/usr/share/dejavu/DejaVuSans.ttf'); so that other can view this as a reference too.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi All,
I got one small problem. I want to set the title in russian language for example. $plot->SetXTitle($lang['Date & Time']); I get funny characters instead.
Two things are needed to make this work: (1) you must be using a TrueType text font which contains the characters you need, and (2) you have to encode your characters correctly in the PHP string you pass to SetXTitle(). (PHPlot doesn't do anything with the characters in your string - it just sends the string to the GD library function.)
So first make sure you are using a TrueType font, using SetDefaultTTFont(), SetUseTTF(), or SetFontTTF(). You want to be using a Unicode font that has the Russian (Cyrillic) characters you need, but that should not be hard. For example, both DejaVu and Liberation open-source TTF fonts should work.
The harder part is going to be the way your text is encoded or escaped. XML decimal numeric encoded sequences work, for example: "€" for a Euro symbol. Also UTF-8 encoded multibyte escapes in strings work, for example "\xe2\x82\xac" also for a Euro. As far as I know, those are the only two ways to encode characters in PHP that work with GD, but I don't know a lot about it.
What exactly do you have in your $lang array?
I want to add that yes, you can have a string in PHP with the actual UTF-8 characters, and they will be plotted correctly. I hadn't tried this before (I have trouble getting those characters in and displaying them in my text editor), but I just tried it and it works. It is in fact documented behavior for the PHP GD function imagettftext(). So if you have a text editor that can handle UTF-8 entry and display, you can enter your characters that way right into PHP string values and they should plot correctly.
Dear Ibayuk,
Let me thank you for your effort. Ok what we are doing is a translation template. So I have php which keep the translation of each word say vehicle in english so is call $lang['Vehicle'] will get the translated term which is keep in the individual language .php for instance russian.php etc. I am using ultraedit to write my php. First the encode was ascii (dos) then I covert it to UTF-8. I could use this method to work for all my other pages. Comes to phplot it does not work. For you information if I convert ascii to unicode all my others goes wrong gets some funny chinese characters rather. I hope this helps.
Yes, I understood that $lang[] was for localization (translation strings), but was wondering if you had UTF-8 strings values in there (vs something else, like decimal encoded entities). With UTF-8 encoded strings in there, it should work.
What about the font? Did you set PHPlot to use a TrueType font for the title(s), and does that font have the characters you need?
If it isn't the font, it could be something in the array isn't what we think, and that is going to be harder to debug.
Can you try the attached file chartest.php? This doesn't use PHPlot, just the underlying PHP/GD routines. You can run it with the PHP CLI (redirect output to file.png), or put it on a web server and access it through the browser. The output is a PNG file with some test strings. (You will need to edit the script to change the font path to something you have; make sure your editor doesn't re-encode the file.)
The output I get is here
Related
Wiki: UTF8-Encoding
Dear Ibayuk,
For you information the ultra edit file which stored localisation is for sure it UTF-8 because else all of it appear as group of question marks ???????. Ok I tried this now $plot->SetDefaultTTFont('phPlot/arial.ttf'); and yes it works. So must I put the arial.ttf file to be there always. What does this do ? Is there anything I must do ?
Great progress, so it looks like a font issue. Yes you will need to make PHPlot use a TrueType font (one which contains the Unicode groups you need) for its text. This is because PHP/GD's built-in fonts are not UTF-8 encoded, and have few non-ASCII characters.
Depending on your server or hosting environment, TrueType fonts should aready be available. You just need to pick one and tell PHPlot to use it with one of the 3 functions I listed.
There are also free fonts you can include with your application if you want. (Arial isn't free, so technically you can't copy it.) This includes the Liberation and DejaVu font families, but there are many others.
More on fonts for PHPlot is here
Dear Ibayuk,
MY server is Centos 6.4. So what do you think I pick from server the arial font? For test I just took the .ttf file from my windows. So what is your best advice here.
CentOS has TrueType fonts available, however I don't know which ones are installed by default or on your server. You can check by looking in /usr/share/fonts (which is where Red Hat and CentOS keep fonts). Inside there will be directories for font families.
On CentOS, /usr/share/fonts/liberation contains the Liberation font family, and /usr/share/fonts/dejavu contains the DejaVu family. If you have them. Both of these are good, and both contain at least some Cyrillic characters. The DejaVu family is bigger (contains many more Unicode characters). The Liberation family is meant as a drop-in replacement for Microsoft's fonts Arial, Times New Roman, and Courier New. You can use whichever you prefer, or any other TrueType font that works for you.
For maximum chance of success, although non-portable to other systems, you can specify a full font file name in PHPlot like this:
This is the Liberation Sans font, similar to Arial. (You should be able to do it without specifying the full path, but it doesn't work in all cases.)
Dear Ibayuk,
Yes I went into /usr/share/fonts/liberation and found a list of it. So is dejavu is better cause I got that too in my system. So I am very new to this but confuse with this TrueType ?
Well, I don't know any Russian so I have no idea what these characters mean and which ones are commonly used or not. If you are a Russian speaker, or have someone doing translations for you, then it is possible to look at the characters available in the font to make a choice. Your server or PC should have an application to look at fonts. On my Linux PCs it is under the menu Accessories > Character Map (program name is "gucharmap"). Select the font name from the pull-down, and be sure to check the box View > Show only glyphs from this font, if you want to see what is available.
But since you have the more complete DejaVu font family, why not just go with that?
That font should support any language you need. It is also available in bold and italic, should you want to use different fonts for titles or labels.
TrueType is a kind of font file. TrueType fonts are usually very high quality (compared to the simple fixed-cell fonts that are built in to PHP/GD). On the other hand, TrueType text is slower to render (draw), so this might be a concern if you have very critical performance issues. Also you might find TrueType font text to be less readable at very small point sizes. So if your plots have small labels with only numbers (where Unicode characters are not an issue), you might choose to use the built-in GD fonts for the labels, and use TrueType for the bigger titles.
Does this help, or confuse things even more?
Dear Ibayuk,
I think this is helpful enough ready $plot->SetDefaultTTFont('/usr/share/dejavu/DejaVuSans.ttf'); so that other can view this as a reference too.