| 1 | #======================================================================= | ||
| 2 | # ____ ____ _____ _ ____ ___ ____ | ||
| 3 | # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \ | ||
| 4 | # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) | | ||
| 5 | # | __/| |_| | _| _ _ / ___ \| __/| | / __/ | ||
| 6 | # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____| | ||
| 7 | # | ||
| 8 | # A Perl Module Chain to faciliate the Creation and Modification | ||
| 9 | # of High-Quality "Portable Document Format (PDF)" Files. | ||
| 10 | # | ||
| 11 | # Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>. | ||
| 12 | # | ||
| 13 | #======================================================================= | ||
| 14 | # | ||
| 15 | # This library is free software; you can redistribute it and/or | ||
| 16 | # modify it under the terms of the GNU Lesser General Public | ||
| 17 | # License as published by the Free Software Foundation; either | ||
| 18 | # version 2 of the License, or (at your option) any later version. | ||
| 19 | # | ||
| 20 | # This library is distributed in the hope that it will be useful, | ||
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 23 | # Lesser General Public License for more details. | ||
| 24 | # | ||
| 25 | # You should have received a copy of the GNU Lesser General Public | ||
| 26 | # License along with this library; if not, write to the | ||
| 27 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 28 | # Boston, MA 02111-1307, USA. | ||
| 29 | # | ||
| 30 | # $Id: UniWrap.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 31 | # | ||
| 32 | #======================================================================= | ||
| 33 | |||
| 34 | package PDF::API3::Compat::API2::UniWrap; | ||
| 35 | |||
| 36 | # Implements UAX#14: Line Breaking Properties | ||
| 37 | # David Nesting <david@fastolfe.net> | ||
| 38 | |||
| 39 | BEGIN { | ||
| 40 | |||
| 41 | use utf8; | ||
| 42 | use Encode qw(:all); | ||
| 43 | |||
| 44 | use 5.008; | ||
| 45 | use strict; | ||
| 46 | use base 'Exporter'; | ||
| 47 | |||
| 48 | use Unicode::UCD; | ||
| 49 | use Carp; | ||
| 50 | |||
| 51 | use vars qw($VERSION); | ||
| 52 | |||
| 53 | ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.0 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2005/11/16 02:16:00 $ | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | no warnings qw[ deprecated recursion uninitialized ]; | ||
| 58 | |||
| 59 | our $DEBUG = 0; | ||
| 60 | our $columns = 75; | ||
| 61 | |||
| 62 | my %classified; | ||
| 63 | my $procedural_self; | ||
| 64 | my $txt; | ||
| 65 | |||
| 66 | use constant PROHIBITED => 0; | ||
| 67 | use constant INDIRECT => 1; | ||
| 68 | use constant DIRECT => 2; | ||
| 69 | use constant REQUIRED => 3; | ||
| 70 | |||
| 71 | my @CLASSES = qw{ OP CL QU GL NS EX SY IS PR PO NU AL ID IN HY BA BB B2 ZW CM }; | ||
| 72 | my %BREAK_TABLE = ( | ||
| 73 | OP => [qw[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ]], | ||
| 74 | CL => [qw[ 2 0 1 1 0 0 0 0 2 1 2 2 2 2 1 1 2 2 0 1 ]], | ||
| 75 | QU => [qw[ 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]], | ||
| 76 | GL => [qw[ 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]], | ||
| 77 | NS => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]], | ||
| 78 | EX => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]], | ||
| 79 | SY => [qw[ 2 0 1 1 1 0 0 0 2 2 1 2 2 2 1 1 2 2 0 1 ]], | ||
| 80 | IS => [qw[ 2 0 1 1 1 0 0 0 2 2 1 2 2 2 1 1 2 2 0 1 ]], | ||
| 81 | PR => [qw[ 1 0 1 1 1 0 0 0 2 2 1 1 1 2 1 1 2 2 0 1 ]], | ||
| 82 | PO => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]], | ||
| 83 | NU => [qw[ 2 0 1 1 1 0 0 0 2 1 1 1 2 1 1 1 2 2 0 1 ]], | ||
| 84 | AL => [qw[ 2 0 1 1 1 0 0 0 2 2 1 1 2 1 1 1 2 2 0 1 ]], | ||
| 85 | ID => [qw[ 2 0 1 1 1 0 0 0 2 1 2 2 2 1 1 1 2 2 0 1 ]], | ||
| 86 | IN => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 1 1 1 2 2 0 1 ]], | ||
| 87 | HY => [qw[ 2 0 1 1 1 0 0 0 2 2 0 2 2 2 1 1 2 2 0 1 ]], | ||
| 88 | BA => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]], | ||
| 89 | BB => [qw[ 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]], | ||
| 90 | B2 => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 0 0 1 ]], | ||
| 91 | ZW => [qw[ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 ]], | ||
| 92 | CM => [qw[ 2 0 1 1 1 0 0 0 2 2 1 1 2 1 1 1 2 2 0 1 ]], | ||
| 93 | ); | ||
| 94 | |||
| 95 | # Convert the table above into a hash that we can use for speedier lookups | ||
| 96 | |||
| 97 | foreach (keys %BREAK_TABLE) { | ||
| 98 | my @t = @CLASSES; | ||
| 99 | $BREAK_TABLE{$_} = { map { shift(@t) => $_ } @{$BREAK_TABLE{$_}} }; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub new { | ||
| 103 | my $pkg = shift; | ||
| 104 | my $self = { @_ }; | ||
| 105 | $self->{line_length} ||= $columns; | ||
| 106 | $self->{break_table} ||= \%BREAK_TABLE; | ||
| 107 | |||
| 108 | $self->{widthfunc} ||= 1; | ||
| 109 | |||
| 110 | bless($self, ref($pkg) || $pkg); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | # This attempts to identify the on-screen length of a given character. | ||
| 115 | # For normal displays, you can generally assume the character has a | ||
| 116 | # length of 1, but some terminals may expand the width of certain | ||
| 117 | # characters, so that extra space needs to be taken into consideration | ||
| 118 | # here so the wrapping occurs at the proper place. | ||
| 119 | |||
| 120 | sub char_length { | ||
| 121 | shift if ref($_[0]); | ||
| 122 | my ($c) = @_; | ||
| 123 | |||
| 124 | if ($c eq 'CM' || $c eq 'ZW') { | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub lb_class { | ||
| 132 | my $self = ref($_[0]) ? shift() : self(); | ||
| 133 | my $code = Unicode::UCD::_getcode(ord $_[0]); | ||
| 134 | my $hex; | ||
| 135 | |||
| 136 | if (defined $code) { | ||
| 137 | $hex = sprintf "%04X", $code; | ||
| 138 | } else { | ||
| 139 | carp("unexpected arg \"$_[1]\" to Text::Wrap::lb_class()"); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | return $classified{$hex} if $classified{$hex}; | ||
| 144 | |||
| 145 | $txt = do "unicore/Lbrk.pl" unless $txt; | ||
| 146 | |||
| 147 | if ($txt =~ m/^$hex\t\t(.+)/m) { | ||
| 148 | print STDERR "< found direct match for $hex = $1 >\n" if $DEBUG > 1; | ||
| 149 | return $classified{$hex} = $1; | ||
| 150 | } else { | ||
| 151 | print STDERR "< no direct match $hex >\n" if $DEBUG > 1; | ||
| 152 | pos($txt) = 0; | ||
| 153 | |||
| 154 | while ($txt =~ m/^([0-9A-F]+)\t([0-9A-F]+)\t(.+)/mg) { | ||
| 155 | print STDERR "< examining $1 -> $2 >\n" if $DEBUG > 1; | ||
| 156 | if (hex($1) <= $code && hex($2) >= $code) { | ||
| 157 | print STDERR "< found range match for $hex = $3 between $1 and $2 >\n" if $DEBUG > 1; | ||
| 158 | return $classified{$hex} = $3; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | return 'XX'; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | # Returns a list of breaking properties for the given text | ||
| 166 | sub text_properties { | ||
| 167 | my $self = ref($_[0]) ? shift() : self(); | ||
| 168 | my ($text) = @_; | ||
| 169 | |||
| 170 | my @characters = split(//, $text); | ||
| 171 | my @classifications = map { $self->lb_class($_) } @characters; | ||
| 172 | |||
| 173 | class_properties(@classifications); | ||
| 174 | } | ||
| 175 | |||
| 176 | # Returns a list of breaking properties for the provided breaking classes | ||
| 177 | sub class_properties { | ||
| 178 | my $self = ref($_[0]) ? shift() : self(); | ||
| 179 | no warnings 'uninitialized'; | ||
| 180 | |||
| 181 | my @breaks; | ||
| 182 | my $last_class = $_[0]; | ||
| 183 | |||
| 184 | $last_class = 'ID' if $last_class eq 'CM'; # broken combining mark | ||
| 185 | |||
| 186 | print STDERR "find_breaks: first class=$last_class\n" if $DEBUG; | ||
| 187 | |||
| 188 | for (my $i = 1; $i <= $#_; $i++) { | ||
| 189 | print STDERR "find_breaks: i=$i class=$_[$i] prev=$last_class breaks[i-1]=$breaks[$i-1]\n" if $DEBUG; | ||
| 190 | $breaks[$i-1] ||= 0; | ||
| 191 | |||
| 192 | $_[$i] = 'ID' if $_[$i] eq 'XX'; # we want as few of these as possible! | ||
| 193 | |||
| 194 | if ($_[$i] eq 'SA') { | ||
| 195 | # TODO: Need a classifiation system for complex characters | ||
| 196 | } | ||
| 197 | |||
| 198 | elsif ($_[$i] eq 'CR') { | ||
| 199 | $breaks[$i] = REQUIRED; | ||
| 200 | } | ||
| 201 | |||
| 202 | elsif ($_[$i] eq 'LF') { | ||
| 203 | if ($_[$i-1] eq 'CR') { | ||
| 204 | $breaks[$i-1] = PROHIBITED; | ||
| 205 | } | ||
| 206 | $breaks[$i] = REQUIRED; | ||
| 207 | } | ||
| 208 | |||
| 209 | elsif ($_[$i] eq 'BK') { | ||
| 210 | $breaks[$i] = REQUIRED; | ||
| 211 | } | ||
| 212 | |||
| 213 | elsif ($_[$i] eq 'SP') { | ||
| 214 | $breaks[$i-1] = PROHIBITED; | ||
| 215 | next; | ||
| 216 | } | ||
| 217 | |||
| 218 | elsif ($_[$i] eq 'CM') { | ||
| 219 | if ($_[$i-1] eq 'SP') { | ||
| 220 | $last_class = 'ID'; | ||
| 221 | if ($i > 1) { | ||
| 222 | $breaks[$i-2] = $self->{break_table}->{$_[$i-2]}->{ID} == | ||
| 223 | DIRECT ? DIRECT : PROHIBITED; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | elsif ($last_class ne 'SP') { | ||
| 229 | if ($breaks[$i-1] != REQUIRED) { | ||
| 230 | my $this_break = $self->{break_table}->{$last_class}->{$_[$i]}; | ||
| 231 | |||
| 232 | if ($this_break == INDIRECT) { | ||
| 233 | $breaks[$i-1] = $_[$i-1] eq 'SP' ? INDIRECT : PROHIBITED; | ||
| 234 | } else { | ||
| 235 | # die "internal error: no table mapping between '$last_class' and '$_[$i]'\n" | ||
| 236 | # unless defined $this_break; | ||
| 237 | if(defined $this_break) | ||
| 238 | { | ||
| 239 | $breaks[$i-1] = $this_break; | ||
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | $breaks[$i-1] = DIRECT; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | $last_class = $_[$i]; | ||
| 250 | } | ||
| 251 | |||
| 252 | # $breaks[$#breaks] = DIRECT; | ||
| 253 | push(@breaks, REQUIRED); | ||
| 254 | |||
| 255 | print STDERR "find_breaks: returning " . join(":", @breaks) . "\n" if $DEBUG; | ||
| 256 | return @breaks; | ||
| 257 | } | ||
| 258 | |||
| 259 | # Returns a list of break points in the provided text, based on | ||
| 260 | # the line length | ||
| 261 | sub find_breaks { | ||
| 262 | my $self = ref($_[0]) ? shift() : self(); | ||
| 263 | my $text = shift; | ||
| 264 | |||
| 265 | no warnings 'uninitialized'; # since we do a lot of subscript +/- 1 checks | ||
| 266 | |||
| 267 | my @characters = split //, $text; | ||
| 268 | |||
| 269 | my @classifications = map { $self->lb_class($_) } @characters; | ||
| 270 | my @lengths = map { $self->char_length($_) } @characters; | ||
| 271 | |||
| 272 | my @breaks = $self->class_properties(@classifications); | ||
| 273 | my @breakpoints; | ||
| 274 | |||
| 275 | my $last_start = 0; | ||
| 276 | my $last_break; | ||
| 277 | my $last_length; | ||
| 278 | my $pos = 0; | ||
| 279 | |||
| 280 | for (my $i = 0; $i <= $#lengths; $i++) { | ||
| 281 | |||
| 282 | print STDERR "[i=$i '$characters[$i]' $classifications[$i] $breaks[$i]] " if $DEBUG; | ||
| 283 | if ($breaks[$i] == REQUIRED) { | ||
| 284 | print STDERR "required breakpoint\n" if $DEBUG; | ||
| 285 | push(@breakpoints, $i+1); | ||
| 286 | $last_start = $i+1; | ||
| 287 | $pos = 0; | ||
| 288 | next; | ||
| 289 | } | ||
| 290 | |||
| 291 | my $c = $pos + $lengths[$i]; | ||
| 292 | |||
| 293 | if ($c > $self->{line_length}) { | ||
| 294 | print STDERR "want to break " if $DEBUG; | ||
| 295 | if (defined $last_break) { | ||
| 296 | print STDERR "at $last_break\n" if $DEBUG; | ||
| 297 | push(@breakpoints, $last_break + 1); | ||
| 298 | $last_start = $last_break + 1; | ||
| 299 | undef $last_break; | ||
| 300 | $pos -= $last_length - 1; | ||
| 301 | print STDERR "[pos now $pos]\n" if $DEBUG; | ||
| 302 | next; | ||
| 303 | } elsif (defined $self->{emergency_break} && $c > $self->{emergency_break}) { | ||
| 304 | print STDERR "NOW\n" if $DEBUG; | ||
| 305 | push(@breakpoints, $i+1); | ||
| 306 | $pos = 0; | ||
| 307 | } else { | ||
| 308 | print STDERR "but can't" if $DEBUG; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | print STDERR "\n" if $DEBUG; | ||
| 312 | |||
| 313 | $last_break = $i if $breaks[$i]; | ||
| 314 | $last_length = $pos if $breaks[$i]; | ||
| 315 | |||
| 316 | $pos += $lengths[$i]; | ||
| 317 | } | ||
| 318 | |||
| 319 | push(@breakpoints, $#lengths) if $breakpoints[$#breakpoints] < $#lengths; | ||
| 320 | |||
| 321 | print STDERR "find_breaks: returning breakpoints " . join(":", @breakpoints) . "\n" if $DEBUG; | ||
| 322 | |||
| 323 | return @breakpoints; | ||
| 324 | } | ||
| 325 | |||
| 326 | # Returns a list of lines, broken up with find_breaks | ||
| 327 | sub break_lines { | ||
| 328 | my $self = ref($_[0]) ? shift() : self(); | ||
| 329 | my $text = shift; | ||
| 330 | |||
| 331 | my @breaks = $self->find_breaks($text); | ||
| 332 | my @lines; | ||
| 333 | |||
| 334 | my $last = 0; | ||
| 335 | foreach (@breaks) { | ||
| 336 | push(@lines, substr($text, $last, $_-$last)); | ||
| 337 | $last = $_; | ||
| 338 | } | ||
| 339 | |||
| 340 | return @lines; | ||
| 341 | } | ||
| 342 | |||
| 343 | 1; | ||
| 344 | |||
| 345 | __END__ |