# $Id: /mirror/trunk/lib/Encode/HanConvert.pm 42 2006-12-12T15:54:31.604008Z kcwu $ package Encode::HanConvert; use vars qw/$VERSION @EXPORT @EXPORT_OK/; $VERSION = '0.34'; @EXPORT = qw( big5_to_gb trad_to_simp big5_to_simp gb_to_trad big5_to_trad gb_to_simp gb_to_big5 simp_to_trad simp_to_big5 trad_to_gb trad_to_big5 simp_to_gb ); @EXPORT_OK = qw(simple trad); use base 'Exporter'; if (eval "use Encode qw|encode decode from_to encode_utf8 decode_utf8|; 1") { require XSLoader; eval { XSLoader::load(__PACKAGE__, $VERSION) } or eval {local $^W; require Encode::HanConvert::Perl; Encode::HanConvert::Perl->import; 1} or die "Can't load Perl-based Converter: $@"; } else { eval {local $^W; require Encode::HanConvert::Perl; Encode::HanConvert::Perl->import; 1} or die "Can't load Perl-based Converter: $@"; } sub big5_to_gb ($) { local $^W; # shuts Encode::HZ's redefine warnings up require Encode::CN; local $_[0] = $_[0] if defined wantarray; from_to($_[0], 'big5-simp' => 'gbk'); return $_[0]; } sub gb_to_big5 ($) { require Encode::TW; local $_[0] = $_[0] if defined wantarray; from_to($_[0], 'gbk-trad' => 'big5'); return $_[0]; } sub trad_to_simp ($) { return decode('trad-simp', encode_utf8($_[0])) if (defined wantarray); $_[0] = decode('trad-simp', encode_utf8($_[0])); } sub simp_to_trad ($) { return decode('simp-trad', encode_utf8($_[0])) if (defined wantarray); $_[0] = decode('simp-trad', encode_utf8($_[0])); } sub big5_to_simp ($) { return decode('big5-simp', $_[0]) if (defined wantarray); $_[0] = decode('big5-simp', $_[0]); } sub simp_to_big5 ($) { return encode('big5-simp', $_[0]) if (defined wantarray); $_[0] = encode('big5-simp', $_[0]); } sub gb_to_trad ($) { return decode('gbk-trad', $_[0]) if (defined wantarray); $_[0] = decode('gbk-trad', $_[0]); } sub trad_to_gb ($) { return encode('gbk-trad', $_[0]) if (defined wantarray); $_[0] = encode('gbk-trad', $_[0]); } # For completeness' sake... sub big5_to_trad ($) { require Encode::TW; return decode('big5', $_[0]) if (defined wantarray); $_[0] = decode('big5', $_[0]); } sub trad_to_big5 ($) { require Encode::TW; return encode('big5', $_[0]) if (defined wantarray); $_[0] = encode('big5', $_[0]); } sub gb_to_simp ($) { local $^W; require Encode::CN; return decode('gbk', $_[0]) if (defined wantarray); $_[0] = decode('gbk', $_[0]); } sub simp_to_gb ($) { local $^W; require Encode::CN; return encode('gbk', $_[0]) if (defined wantarray); $_[0] = encode('gbk', $_[0]); } # Lingua::ZH::HanConvert drop-in replacement -- not exported by default sub trad { simp_to_trad($_[0]) }; sub simple { trad_to_simp($_[0]) }; 1; __END__ =head1 NAME Encode::HanConvert - Traditional and Simplified Chinese mappings =head1 VERSION This document describes version 0.34 of Encode::HanConvert, released July 17, 2007. =head1 SYNOPSIS As command line utilities: B [ B<-p> ] [ B<-u> ] [ I ...] > I B [ B<-p> ] [ B<-u> ] [[ I ...] > I In your program: # The XS-based implementation needs Encode.pm 1.41; # otherwise, autoloads the Perl-based Encode::HanConvert::Perl use Encode::HanConvert; # Conversion between Chinese encodings $gbk = big5_to_gb($big5); # Big5 to GBK $big5 = gb_to_big5($gbk); # GBK to Big5 # Conversion between Perl's Unicode strings $simp = trad_to_simp($trad); # Traditional to Simplified $trad = simp_to_trad($simp); # Simplified to Traditional # Conversion between Chinese encoding and Unicode strings $simp = big5_to_simp($big5); # Big5 to Simplified $big5 = simp_to_big5($simp); # Simplified to Big5 $trad = gb_to_trad($gbk); # GBK to Traditional $gbk = trad_to_gb($trad); # Traditional to GBK # For completeness' sake... (no conversion, just encode/decode) $simp = gb_to_simp($gbk); # GBK to Simplified $gbk = simp_to_gb($simp); # Simplified to GBK $trad = big5_to_trad($big5); # Big5 to Traditional $big5 = trad_to_big5($trad); # Traditional to Big5 # All functions may be used in void context to transform $_[0] big5_to_gb($string); # convert $string from Big5 to GBK # Drop-in replacement functions for Lingua::ZH::HanConvert use Encode::HanConvert qw(trad simple); # not exported by default $simp = simple($trad); # Traditional to Simplified $trad = trad($simp); # Simplified to Traditional =head1 DESCRIPTION This module is an attempt to solve most common problems occured in Traditional vs. Simplified Chinese conversion, in an efficient, flexible way, without resorting to external tools or modules. If you are using perl 5.7.2 or earlier, all Unicode-related functions are disabled, and B is automagically loaded and used instead. In that case, please consult L instead. After installing this module, you'll have two additional encoding formats: C maps I into Unicode's Simplified Chinese (and vice versa), and C maps I (also known as I) into Unicode's Traditional Chinese and back. The module exports various C functions by default, where xxx and yyy are one of C, C (i.e. GBK/CP936), C (simplified Chinese unicode), or C (traditional Chinese unicode). You may also import C and C, which are aliases for C and C; this is provided as a drop-in replacement for programs using L. Since this is built on L's architecture, you may also use the line discipline syntax to perform the conversion implicitly (before 5.7.3, you need to use 'cp936' in place of 'gbk'): require Encode::CN; open BIG5, ':encoding(big5-simp)', 'big5.txt'; # as simplified open EUC, '>:encoding(gbk)', 'gbk.txt'; # as gbk print EUC, ; require Encode::TW; open EUC, ':encoding(gbk-trad)', 'gbk.txt'; # as traditional open BIG5, '>:encoding(big5)', 'big5.txt'; # as big-5 print BIG5, ; Or, more interestingly: =for encoding big5 use encoding 'big5-simp'; print "¤¤¤å"; # prints simplified Chinese in unicode =head1 COMPARISON Although L module already provides mapping between Simplified and Traditional Unicode characters, it depend on other modules (L or L) to provide the necessary mapping with B and B encodings. Also, L loads up much faster: 0.04 real 0.03 user 0.01 sys # Encode::HanConvert 0.19 real 0.18 user 0.00 sys # Encode::HanConvert::Perl 1.68 real 1.66 user 0.01 sys # Lingua::ZH::HanConvert (v0.12) The difference in actual conversion is much more significant. Use 5mb text of trad => simp as an example: 0.77 real 0.25 user 0.00 sys # iconv | b2g | iconv 0.64 real 0.59 user 0.04 sys # Encode::HanConvert b2g.pl -u 13.79 real 13.69 user 0.02 sys # Lingua::ZH::HanConvert trad2simp (v0.12) The C above refers to Yeung and Lee's I, a C-based program that maps big5 to gb2312 and back; C refers to GNU libiconv. If you don't mind the overhead of calling an external process, their result is nearly identical with this module; however, their map falls short on rarely-used characters and box-drawing symbols. =head1 CAVEATS Please note that from version 0.03 and above, this module support the more expressive range B instead of B. This may cause incompatibilities with older fonts. Programs using an earlier version of this module should rename C into C; sorry for the inconvenience. This module does not preserve one-to-many mappings; it blindly chooses the most frequently used substitutions, instead of presenting the user multiple choices. This can be remedied by a dictionary-based post processor that restores the correct character. As of version 0.05, the mapping from Big5 to GBK is I: All displayable Big5 characters are mapped, although substitute characters are used where there's no direct corresponding characters. However, there are numerous GBK characters without its Big5 counterparts: C from the distribution directory should show all of them. Any help on completing this mapping are very appreciated. =head1 ACKNOWLEDGEMENTS The conversion table used in this module comes from various sources, including B by David Chan, B by Ricky Yeung & Fung F. Lee, and B from Doggy Digital Creative Inc. (L), Rei-Li Chen (rexchen), Unicode consortium's Unicode Character Database (L), as well as mappings used in Microsoft Word 2000, Far East edition. The F<*.ucm> files are checked against test files generated by GNU libiconv with kind permission from Bruno Haible. Kudos to Nick Ing-Simmons, Dan Kogai and Jarkko Hietaniemi for showing me how to use B and PerlIO. Thanks! =head1 SEE ALSO L, L, L, L The L and L utilities installed with this module. =head1 AUTHORS Currently maintained by Kuang-che Wu Ekcwu@csie.orgE. Orignal author: Audrey Tang Ecpan@audreyt.orgE =head1 COPYRIGHT Copyright 2002, 2003, 2004, 2007 by Audrey Tang Ecpan@audreyt.orgE. Copyright 2006 by Kuang-che Wu Ekcwu@csie.orgE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut