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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
# Copyright (C) 2013 MURATA Yasuhisa
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package MIME::EcoEncode::Fold;
use 5.008005;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw($VERSION);
our @EXPORT = qw(mime_eco_fold);
our $VERSION = '0.95';
our $LF; # line feed
our $BPL; # bytes per line
our $UTF8;
our $REG_W;
our $SPL;
sub mime_eco_fold {
my $str = shift;
return '' unless defined $str;
return '' if $str eq '';
my $charset = shift || 'UTF-8';
my $cs;
if ($charset =~ /^([-0-9A-Za-z_]+)$/i) {
$cs = lc($1);
}
else { # invalid option
return undef;
}
our $LF = shift || "\n "; # line feed
our $BPL = shift || 990; # bytes per line
our $UTF8 = 1;
our $REG_W = qr/(.)/;
$LF =~ /([^\x0d\x0a]*)$/;
our $SPL = length($1);
my $jp = 0;
if ($cs ne 'utf-8') {
$UTF8 = 0;
if ($cs eq 'iso-2022-jp') {
$jp = 1;
}
elsif ($cs eq 'shift_jis') {
# range of 2nd byte : [\x40-\x7e\x80-\xfc]
$REG_W = qr/([\x81-\x9f\xe0-\xfc]?.)/;
}
elsif ($cs eq 'gb2312') { # Simplified Chinese
# range of 2nd byte : [\xa1-\xfe]
$REG_W = qr/([\xa1-\xfe]?.)/;
}
elsif ($cs eq 'euc-kr') { # Korean
# range of 2nd byte : [\xa1-\xfe]
$REG_W = qr/([\xa1-\xfe]?.)/;
}
elsif ($cs eq 'big5') { # Traditional Chinese
# range of 2nd byte : [\x40-\x7e\xa1-\xfe]
$REG_W = qr/([\x81-\xfe]?.)/;
}
else { # Single Byte (Latin, Cyrillic, ...)
;
}
}
my $result = '';
my $refsub = $jp ? \&line_fold_jp : \&line_fold;
my $odd = 0;
for my $line (split /(\x0d?\x0a|\x0d)/, $str) {
if ($odd) {
$result .= $line;
$odd = 0;
}
else {
$result .= &$refsub($line);
$odd = 1;
}
}
return $result;
}
sub line_fold {
my $str = shift;
return '' if $str eq '';
my $str_len = length($str);
our $BPL;
return $str if $str_len <= $BPL;
our $LF;
our $UTF8;
our $REG_W;
our $SPL;
my $w = '';
my $w_len;
my $w_bak = '';
my $result = '';
my $max_len = $BPL;
my ($chunk, $chunk_len) = ('', 0);
my $str_pos = 0;
utf8::decode($str) if $UTF8; # UTF8 flag on
while ($str =~ /$REG_W/g) {
$w = $1;
utf8::encode($w) if $UTF8; # UTF8 flag off
$w_len = length($w); # size of one character
if ($chunk_len + $w_len > $max_len) {
$result .= $chunk . "$LF";
$str_pos += $chunk_len;
$max_len = $BPL - $w_len - $SPL;
if ($str_len - $str_pos <= $max_len) {
utf8::encode($str) if $UTF8; # UTF8 flag off
$chunk = substr($str, $str_pos);
last;
}
$chunk = $w;
$chunk_len = $w_len;
}
else {
$chunk .= $w;
$chunk_len += $w_len;
}
}
return $result . $chunk;
}
sub line_fold_jp {
my $str = shift;
return '' if $str eq '';
our $BPL;
return $str if length($str) <= $BPL;
our $LF;
our $SPL;
my $k_in = 0; # ascii: 0, zen: 1 or 2, han: 9
my $k_in_bak = -1;
my $k_out;
my $ec;
my $w1;
my $w1_bak = '';
my $w = '';
my $w_len;
my $w_bak = '';
my $result = '';
my $max_len = $BPL;
while ($str =~ /(\e(..)|.)/g) {
($w1, $ec) = ($1, $2);
$w .= $w1;
if (defined $ec) {
$w1_bak = $w1;
if ($ec eq '(B') {
$k_in = 0;
}
elsif ($ec eq '$B') {
$k_in = 1;
}
else {
$k_in = 9;
}
next;
}
else {
if ($k_in == 1) {
$k_in = 2;
next;
}
elsif ($k_in == 2) {
$k_in = 1;
}
}
$k_out = $k_in ? 3 : 0; # 3 is "\e\(B"
if (pos($str) + $k_out > $max_len) {
$w_len = length($w);
if ($k_in_bak) {
$result .= $w_bak .
substr($str, 0, pos($str) - $w_len, "") . "\e\(B$LF";
if ($k_in) {
if ($k_in_bak == $k_in) {
$w = $w1_bak . $w;
}
}
else {
$w = $w1;
}
}
else {
$result .= $w_bak .
substr($str, 0, pos($str) - $w_len, "") . "$LF";
}
substr($str, 0, $w_len, "");
$max_len = $BPL - length($w) - $SPL;
if (length($str) <= $max_len) {
return $result . $w . $str;
}
$w_bak = $w;
}
$k_in_bak = $k_in;
$w = '';
}
return $result . $w_bak . $str; # impossible
}
1;
|