source: A2P/a2p/A2P/Common.pm @ 3

Last change on this file since 3 was 3, checked in by guillaume, 17 years ago
  • AUTHORS: Ajout des différents contributeurs
  • COPYING: Ajout de la licence GPL v3
  • a2p: Préparation des sources pour leur publication sous GPL
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1#
2# Copyright (c) 2004-2007 - Consultas, PKG.fr
3#
4# This file is part of A2P.
5#
6# A2P is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# A2P is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with A2P; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# $Id: Common.pm 3 2007-10-18 16:20:19Z guillaume $
21#
22# Some common subfunctions useful to A2P and Convertion
23#
24
25package A2P::Common;
26
27use strict;
28use A2P::Globals;
29use A2P::Syslog;
30
31BEGIN {
32    use Exporter ();
33    our ( $VERSION , @ISA , @EXPORT_OK );
34    $VERSION = sprintf "%s", q$Rev: 415 $ =~ /(\d[0-9.]+)\s+/ ;
35    @ISA = qw(Exporter);
36    @EXPORT_OK = qw(&validate &debug &initdebug &closedebug);
37}
38our $VERSION ;
39
40# Private Global Variables
41my @CHARTABLE = ();
42
43# Sub only used for compatibility with AFP2PRINT V1
44sub debug ;
45*debug = \&Debug ;
46
47sub initdebug {
48    $LOGFILENAME  = shift  ;
49    $LOGFILENAME .= '.log' ;
50}
51
52sub closedebug {}
53
54sub validate (\$ $) {
55    my $buffer = shift ;
56    my $nospacestrip = shift ;
57
58    my $space = chr(0x12); # Space mapping for overlays
59    $$buffer =~ s/$space/ /g ;
60    # Strip ending spaces
61    $$buffer =~ s/\s+$// if (!defined($nospacestrip));
62
63    return if ( length($$buffer) == 0 );
64
65    my $index = 0 ;
66    my $SPCcount = 0 ;
67
68    # Pre-computing for TeX and AFP texts
69    if ( $$buffer =~ /[\\\';\-]/ ) {
70        # Start to replace backslash as backslashs will be inserted
71        $$buffer =~ s/\\/\\textbackslash{}/g ;
72        # Substitutions made in AFP text
73        $$buffer =~ s/\'\'/\'/g ; #'
74        $$buffer =~ s/;;/;/g ;
75    }
76
77    # Return in case string is only with good chars
78    return if ( $$buffer =~ /^[a-zA-Z0-1\\;\-:=?\*\+\,]*$/ );
79
80    # Otherwise split the string and compute it again by converting needed chars from CHARTABLE
81    &Debug("Have string  '$$buffer' to validate");
82
83    # Split also on J and 7 that could be TeX redefined by FM12 font
84    my @temp = split( /(?![a-zA-IK-Z0-689])/ , $$buffer );
85
86    #&Debug("Buffer splitted in " . @temp . " parts, beginning with chars " .
87    #  join(", ",map { sprintf("0x%2X",ord($_)) } @temp ));
88    $$buffer = "" ;
89    foreach my $string ( @temp ) {
90        my $chr = ord( $string );
91        if (defined($CHARTABLE[$chr])) {
92            #&Debug("Char " . sprintf("0x%2X",$chr) . " defined to " . $CHARTABLE[$chr]);
93            if ( $CHARTABLE[$chr] eq '\SPC' ) {
94                $SPCcount ++ ;
95                next if (length($string) == 1 );
96            }
97
98            $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;
99
100            $$buffer .= $CHARTABLE[$chr] if ( $CHARTABLE[$chr] ne '\SPC' );
101
102            # We must protect any TeX token as necessary
103            $$buffer .= "{}" if ( $CHARTABLE[$chr] =~ /^\\.+/ and $CHARTABLE[$chr] ne '\SPC' );
104
105            $$buffer .= substr( $string , 1 ) if (length($string) > 1 );
106
107        } else {
108            #&Debug("Char " . sprintf("0x%2X",$chr) . " not defined in table");
109            $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;
110            $$buffer .= $string ;
111            }
112        }
113    $$buffer .= ($SPCcount > 1 ?"\\LSP\{" . $SPCcount . "\}":'\SPC{}') , $SPCcount = 0 if $SPCcount ;
114    $$buffer =~ s/;;/;/g ; # For some overlay validation
115    &Debug("Validated to '$$buffer'");
116}
117
118#=======================================================================
119# initialisation code - stuff the DATA into the CHARTABLE hash
120#=======================================================================
121{
122    my ( $code , $texcode , $comment );
123
124    &Debug("Loading CHARTABLE...");
125    while (<DATA>)
126    {
127        next unless /\S/;
128        chop;
129        s/^\s+//;
130        ( $code, $texcode , $comment ) = split(/\s+/, $_ , 3);
131        next if ( $code =~ /^skip/ );
132        $code = eval($code) if ( $code =~ /^0x/ );
133        $CHARTABLE[$code] = $texcode;
134        #&Debug("Char " . sprintf("0x%2X",$code) . " defined to '" . $CHARTABLE[$code] . "'");
135    }
136
137    close(DATA);
138}
139
140&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");
141
1421;
143
144__DATA__
1450x00             \SPC
1460x01             \SPC
1470x02             \SPC
1480x03             \SPC
1490x04             \SPC
1500x05             \SPC
1510x06             \SPC
1520x07             \SPC
1530x08             \SPC
1540x09             \SPC
1550x0a             \SPC
1560x0b             \SPC
1570x0c             \SPC
1580x0d             \SPC
1590x0e             \SPC
160
1610x0f             \char"27           Used to valid string value from AFP overlays
162
1630x10             \textbackslash     Used to valid string value from AFP overlays
1640x11             \char"2A           Used to valid string value from AFP overlays
165skip 0x12        \SPC         (Was used to valid string value from AFP overlays)
1660x13             \char"28           Used to valid string value from AFP overlays
1670x14             \char"29           Used to valid string value from AFP overlays
1680x15             \char"3F           Used to valid string value from AFP overlays
1690x16             \char"2E           Used to valid string value from AFP overlays
1700x17             \char"2B           Used to valid string value from AFP overlays
1710x18             \textasciicircum   Used to valid string value from AFP overlays
1720x19             \$                 Used to valid string value from AFP overlays
1730x1a             \char"5B           Used to valid string value from AFP overlays
1740x1b             \char"5D           Used to valid string value from AFP overlays
1750x1c             \{                 Used to valid string value from AFP overlays
1760x1d             \}                 Used to valid string value from AFP overlays
1770x1e             \char"2D           Used to valid string value from AFP overlays
1780x1f             \char"7C           Used to valid string value from AFP overlays
179
1800x20            \SPC                space
1810x21            \char"21            !
1820x22            \textquotedbl       "
1830x23            \#
1840x24            \$
1850x25            \%
1860x26            \&
1870x27            \char"27            '
188skip 0x28                           (
189skip 0x29                           )
190skip 0x2a                           *
191skip 0x2b                           +
192skip 0x2c                           ,
1930x2d            \char"2D            -
194skip 0x2e                           .
1950x2f            \ourslash           /
196
1970x37           \cSev                7
198
199skip 0x3a                           :
200skip 0x3b                           ;
2010x3c           \char"3C             <
202skip 0x3d           $=$             =
2030x3e           \char"3E             >
204skip 0x3f                           ?
205
206skip 0x40                           @
207
2080x4A           \cJ                  J
209
2100x5b           ffl                  [
2110x5c           \textbackslash       \
2120x5d           \textquotedblleft    ]
2130x5e           \textasciicircum     ^                Does not exist in IBM chars
2140x5f           \_
215
216skip 0x60                           `
2170x7b           \{
2180x7c           !                    IBM<!>=4F -> Latin1=7C and not \textbar    |
2190x7d           \}
2200x7e           \textasciitilde      ~
2210x7f           \SPC
222
2230x80           \SPC
2240x81           \SPC
2250x82           \SPC
2260x83           \SPC
2270x84           \textquotedblright
2280x85           \SPC
2290x86           \char"86 or \dag
2300x87           \char"87 or \ddag
2310x88           \SPC
2320x89           \SPC
2330x8a           \SPC
2340x8b           \SPC
2350x8c           \SPC
2360x8d           \SPC
2370x8e           \SPC
2380x8f           \SPC
239
2400x90           \SPC
2410x91           \SPC
2420x92           \SPC
2430x93           \SPC
2440x94           \SPC
2450x95           \SPC
2460x96           \SPC
2470x97           \SPC
2480x98           \SPC
2490x99           \SPC
2500x9a           \SPC
2510x9b           \SPC
2520x9c           \SPC
2530x9d           \SPC
2540x9e           \SPC
2550x9f           \SPC                         IBM chars not used
256
2570xA0            \SPC
2580xA1            \textexclamdown
2590xA2            \char"5B or $\lbrack$       IBM<[>=4A -> Latin1=A2 and not <?>
2600xA3            \char"A3
2610xA4            \ourbullet                  Euro symbol in ASCII
2620xA5            \SPC
2630xA6            \textbar
2640xA7            \char"A7
2650xA8            \textquoteleft
2660xA9            \char"A9
2670xAA            \char"96\char"96\setbox4\hbox{\char"96}\kern0.5\wd4   Used to replace double zero for numbers decimal part
2680xAB            \guillemotleft
2690xAC            \char"A2 or $\not\subset$
2700xAD            \ourtop or $top$
2710xAE            \char"AE
2720xAF            \textquoteleft
273
2740xB0            \char"B0
2750xB1            $)$
2760xB2            $\dashv$
2770xB3            \ourtextbar or  $\mid$
2780xB4            \textquoteright
2790xB5            \char"B5 or $\mu$
280skip 0xB6
281skip 0xB7
2820xB8            \SPC
2830xB9            \ourbot or $\bot$
284skip 0xBA
2850xBB            \guillemotright
2860xBC            \char"BC
2870xBD            \char"BD
2880xBE            \char"BE
2890xBF            \char"BF
290
291skip 0xC0
292skip 0xC1
293skip 0xC2
294skip 0xC3
295skip 0xC4
296skip 0xC5
297skip 0xC6
298skip 0xC7
299skip 0xC8
300skip 0xC9
301skip 0xCA
302skip 0xCB
303skip 0xCC
304skip 0xCD
305skip 0xCE
306skip 0xCF
307
3080xD0            \oe
309skip 0xD1
310skip 0xD2
311skip 0xD3
312skip 0xD4
313skip 0xD5
314skip 0xD6
3150xD7            \ourline                    Used in IBM to make lines
3160xD8            \ourOslash                  Used in E-Service
317skip 0xD9
318skip 0xDA
319skip 0xDB
320skip 0xDC
3210xDD            \char"20
3220xDE            \ddag
323skip 0xDF
324
325skip 0xE0
326skip 0xE1
327skip 0xE2
328skip 0xE3
329skip 0xE4
330skip 0xE5
331skip 0xE6
332skip 0xE7
333skip 0xE8
334skip 0xE9
335skip 0xEA
336skip 0xEB
337skip 0xEC
338skip 0xED
339skip 0xEE
340skip 0xEF
341
3420xF0            \dh
343skip 0xF1
344skip 0xF2
345skip 0xF3
346skip 0xF4
347skip 0xF5
348skip 0xF6
3490xF7            \char"F7 or $\div$
350skip 0xF8
351skip 0xF9
352skip 0xFA
353skip 0xFB
354skip 0xFC
355skip 0xFD
356skip 0xFE
3570xFF            \char"FF
Note: See TracBrowser for help on using the repository browser.