Sample Code 268: How to display math and greek characters using symbol font - Part 1?
Written by kksou   
Tuesday, 19 June 2007
Problem

This is in response to pbm's post titled "Symbol font in PHP-GTK2"

Suppose you want to display math or greek characters on windows platform as shown below:

How to display math and greek characters using symbol font - Part 1?


Solution

On windows, if you have tried displaying math or greek characters using Symbol font, you will get the error message
Pango-WARNING **: Couldn't load font "Symbol 12" falling back to "Sans 12"

The main reason is that Pango works only on unicode, but the standard Symbol font on windows does not have a unicode character mapping.

So to display math and greek characters on windows platform, all you need is to find some symbol-equivalent truetype font that supports unicode.

Below are some pointers that may save you some time:

  • OpenOffice comes with a symbol font (opensymbol.ttf) that supports unicode. You can find a copy of this font here: http://packages.debian.org/stable/x11/ttf-opensymbol
  • Download and unpack the package and you will find the ttf file opens___.ttf. The package is of the type .deb. deb packages can be extracted with the command:
    $dpkg-deb -x {deb-package name} {target directory}
    Of course the above works only from linux. I'm not too sure if there's any unzip utility that can extract .deb archive on windows.

  • If you have problem extracting the .deb package, don't worry. I found a website that provides direct download of the OpenSymbol truetype font. It's at: http://fonts.goldenweb.it/index_file/l/en/d2/Freeware_fonts/c/o/start/50/default.html On that page, look for the font called Opensymbol and download the file. It's about 100K.
  • Although both filenames are the same, I found that the one from Debian contains much more font characters than the one from fonts.goldenweb.it. So if you know how to extract .deb package, go for the first one.
  • Once you have downloaded the OpenSymbol ttf file, install the fonts on your windows. Or you can just copy it into your fonts directory (usually it's c:\windows\fonts).
  • Once you have installed the font, you can start displaying math and greek characters right inside php-gtk2 using the familiar method GtkWidget::modify_font() as shown in the sample code below.
  • However, one very important note here. If you are used to Microsoft Word, you can just type "abcde", choose Symbol font, and they will automatically be converted to "αβ&gammaδε".
  • To display math or greek characters using OpenSymbol truetype font, you need to specify the characters in unicode.
  • In this example, I want to focus on displaying the math or greek characters first. If you do not know how to enter unicode characters, please refer to How to display math and greek characters using symbol font - Part 2 - input unicode characters?
  • Since it is difficult to reproduce unicode characters on the web, I have url-encoded the unicode characters so that you have no problem running the sample code below. As long as you have installed the OpenSymbol truetype font as outlined above, you should have no problem running the sample code.
  • There is one last, but extremely important, step you need to do to be able to display the math and greek characters — setting the codepage. Load the php.ini in your favorite text editor. First look for the text codepage. If are using standard English windows, most likely you will see
    php-gtk.codepage = CP1250
    To be able to view the unicode characters, you need to change this to:
    php-gtk.codepage = UTF-8

    Now run the sample code below. you should be able to see the math and greek characters!


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Display Math and Greek Characters\n".
"using Symbol Truetype Font on windows\n".
"Part 1 - Display the Font");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 60);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// display greek characters
$greek_str = urldecode("%EE%82%B7%EE%82%B8%EE%82". // note 1
"%B9%EE%82%BA%EE%82%BB%EE%82%BC%EE%83%81%EE%83%82". 
"%EE%83%86%EE%83%89"); 
$greek_label = new GtkLabel($greek_str);
$greek_label->modify_font(new PangoFontDescription('OpenSymbol 12')); // note 2
$vbox->pack_start($greek_label);

  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Some greek characters in unicode. Note that I have urlencoded the characters. Hence the need to urldecode them.
  2. Set the font to OpenSymbol.
  3. Some math characters in unicode. Note that I have urlencoded the characters. Hence the need to urldecode them.
  4. Set the font to OpenSymbol.

Note

There are also some additional unicode math fonts available here: http://support.wolfram.com/mathematica/systems/windows/general/latestfonts.html


Related Links

User reviews   Average user ratings:    0.0   (from 1 user)
  1. pbm
    June 18, 2007 7:42pm

    very very useful. PHP-GTK2 has a bright future for science apps....

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved