SVG, Unicode, and XSLT - Part 3 of Chapter 7 from Perl Graphics Programming (2/5) | WebReference

SVG, Unicode, and XSLT - Part 3 of Chapter 7 from Perl Graphics Programming (2/5)

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Perl Graphics Programming, Chapter 7: Creating SVG with Perl

If your text input device supports the input of UTF-8 characters, you should be able to include the Unicode characters directly in your Perl code. If not, you can use NCR (Numeric Character References) notation. An NCR can be expressed as a hex value:

א           # The Hebrew aleph

or as a decimal value:

א

If you need to look up a particular character, the Unicode Consortium provides charts that give the hex values for each set of characters.

All that remains is to make sure you have a font that has glyphs for the character range you wish to display (not all fonts include all glyphs). Again, the Unicode Consortium maintains a list of resources for finding fonts for particular platforms that can render certain character ranges.

Example 7-2 displays some Katakana characters (in the range 0x30A0 to 0x30FF) underneath the Latin characters "O'Reilly". (Katakana characters are Japanese glyphs for representing foreign words phonetically.) To display the Katakana characters, you need a font like MSGothic (Windows), AppleGothic (Mac), or Caslon (Unix). A collection of Unicode-enabled fonts is available at Alan Wood's Unicode Resources page http://www.hclrss.demon.co.uk/unicode.

Example 7-2: Using Unicode characters in SVG

#!/usr/bin/perl -w
# 
# Example 7-2. Unicode in SVG
   
use strict;
use XML::Writer;    # Used to write the SVG output
use IO::Scalar;     # Used to buffer the XML::Writer output
   
my $output = new IO::Scalar;
my $writer = XML::Writer->new(OUTPUT => $output);
$writer->setDataIndent(2);
   
$writer->xmlDecl("UTF-8");   
                            
$writer->startTag('svg',
                  width => 200,
                  height => 100,
                  'xmlns:xlink' => 'http://www.w3.org/1999/Xlink');
$writer->startTag('text',
                  x => 100,
                  y => 50,
                  'font-family' => 'Arial',
                  'font-size' => 40,
                  'text-anchor' => 'middle',
                   fill   => '#000000');
$writer->characters("O'Reilly");
$writer->startTag('tspan',
                  'font-family' => 'Gothic MS',
                  'font-size' => 24,
                   x => 100, dy => 24);
   
# Add Katakana glyphs 
   
print $output "オライリー";
$writer->endTag('tspan'); 
$writer->endTag('text'); 
$writer->endTag('svg');
   
print $output;

Technically, we should always provide the XML header specifying the charset used in this document (in this case, UTF-8). The xmlDecl( ) function adds a header that looks like this:

<?xml version="1.0" encoding="UTF-8"?>

Figure 7-3 illustrates the output. For more on Perl and Unicode see the Perl/Unicode FAQ at http://rf.net/~james/perli18n.html.

Text in Katakana and Latin characters
Figure 7-3. Text in Katakana and Latin characters


To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Created: February 26, 2003
Revised: February 26, 2003

URL: http://webreference.com/programming/perl/chap7/3/2.html