8.7. Inserting Unicode entities

One reason for using TTF fonts is the possibility to inserting unicode character/entities. With this we mean characters from the extended range that are not normally available on West european keyboards. This could for example be classical Greek characters often used in mathematics, for example the symbol for "pi". In order to make these and other commonly used "special" characters more accessible the library provides a utility class that makes it easy to use these characters without having to look up the corresponding unicode entities every time.

In order to specify characters not available on the keyboard the normal way is to specify there unicode code and included it in the text string with the prefix "&#". For example the unicode for the character "pi" is "03C0" in hex so to include this character in a text string you would have to write "This is pi π" note that the code should be given in decimal encoding in the string and always use 4 digits (pre-padded with 0:s as necessary). Since it is very tedious to lookup and encode special characters the library offers a simpler way. The SymChar class.

Note

This section can be skipped at first reading without loss of continuity.

8.7.1. The utility class "SymChar"

As described above it is tedious to have to lookup all the character codes. To simplify this the SymChar class allows you to find these characters with the english common name of these symbols instead. For example to create a string with the Greek character "pi" one would have to write the code as shown in.

Example 8.2. Using the SymChar class to display the Greek letter "pi"

1
2
3
4
5
6
<?php
// ...
$pi = SymChar::Get('pi');
$graph->title->Set('Test is pi'.$pi);
// ...
?>

All supported entities with there symbolic names are listed in Table 8.3. The first argument to the static method SymChar::Get() should be the name of the entity and if the second optional argument is true then the capital version (if it exists) of the symbol should be returned.

Table 8.3. Supported character entities in class SymChar

NameUnicode Unicode capitalComment
"alpha"03B10391Greek character
"beta"03B20392Greek character
"gamma"03B30393Greek character
"delta"03B40394Greek character
"epsilon"03B50395Greek character
"zeta"03B60396Greek character
"ny"03B70397Greek character
"eta"03B80398Greek character
"theta"03B80398Greek character
"iota"03B90399Greek character
"kappa"03BA039AGreek character
"lambda"03BB039BGreek character
"mu"03BC039CGreek character
"nu"03BD039DGreek character
"xi"03BE039EGreek character
"omicron"03BF039FGreek character
"pi"03C003A0Greek character
"rho"03C103A1Greek character
"sigma"03C303A3Greek character
"tau"03C403A4Greek character
"upsilon"03C503A5Greek character
"phi"03C603A6Greek character
"chi"03C703A7Greek character
"psi"03C803A8Greek character
"omega"03C903A9Greek character
"euro"20AC Monetary symbol
"yen"00A5 Monetary symbol
"pound"20A4 Monetary symbol
"approx"2248 Mathematical
"neq"2260 Mathematical
"not"2310 Mathematical
"def"2261 Mathematical
"inf"221E Mathematical
"sqrt"221A Mathematical
"int"222B Mathematical
"copy"00A9 Misc symbols
"para"00A7 Misc symbols
"tm"2122 Misc symbols
"rtm"00AE Misc symbols


Figure 8.6 and Figure 8.7 shows how these symbol look when they are rendered with the TTF font Times Roman 14pt. The symbols are in the same order as in the table above and are visually grouped by there category as specified in the column "Comment" in the table above.

Figure 8.6. Rendered symbol characters corresponding to

Rendered symbol characters corresponding to


Figure 8.7. Rendered capital symbol characters corresponding to Table 8.3.

Rendered capital symbol characters corresponding to .


8.7.2. Graph example with Greek labels

In Figure 8.8 we have used the previous discussed SymChar class to more readily insert "π" characters in the X-axis label. We highlight some functionality that we haven't yet discussed in detail

  1. We use a utility class (FuncGenerator available in "jpgraph_utils.inc.php") to help create plot values from a mathematical expression. This will help create a set of (x,y) points that can later on be used as the base for a data series.

  2. The x-axis labels and the position of the tick marks on the x-axis are manually positioned and specified to be positioned at "even" fractions of π.

Figure 8.8. Specifying manual ticks as fraction of Pi. (manualtickex2.php)

Specifying manual ticks as fraction of Pi. (manualtickex2.php)