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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
#=======================================================================
# ____ ____ _____ _ ____ ___ ____
# | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
# | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
# | __/| |_| | _| _ _ / ___ \| __/| | / __/
# |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
#
# A Perl Module Chain to faciliate the Creation and Modification
# of High-Quality "Portable Document Format (PDF)" Files.
#
# Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>.
#
#=======================================================================
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# $Id: UniWrap.pm,v 1.3 2005/03/14 22:01:06 fredo Exp $
#
#=======================================================================
package PDF::API2::UniWrap;
# Implements UAX#14: Line Breaking Properties
# David Nesting <david@fastolfe.net>
BEGIN {
use utf8;
use Encode qw(:all);
use 5.008;
use strict;
use base 'Exporter';
use Unicode::UCD;
use Carp;
use vars qw($VERSION);
( $VERSION ) = '$Revision: 1.3 $' =~ /Revision: (\S+)\s/; # $Date: 2005/03/14 22:01:06 $
}
no warnings qw[ deprecated recursion uninitialized ];
our $DEBUG = 0;
our $columns = 75;
my %classified;
my $procedural_self;
my $txt;
use constant PROHIBITED => 0;
use constant INDIRECT => 1;
use constant DIRECT => 2;
use constant REQUIRED => 3;
my @CLASSES = qw{ OP CL QU GL NS EX SY IS PR PO NU AL ID IN HY BA BB B2 ZW CM };
my %BREAK_TABLE = (
OP => [qw[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ]],
CL => [qw[ 2 0 1 1 0 0 0 0 2 1 2 2 2 2 1 1 2 2 0 1 ]],
QU => [qw[ 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]],
GL => [qw[ 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]],
NS => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]],
EX => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]],
SY => [qw[ 2 0 1 1 1 0 0 0 2 2 1 2 2 2 1 1 2 2 0 1 ]],
IS => [qw[ 2 0 1 1 1 0 0 0 2 2 1 2 2 2 1 1 2 2 0 1 ]],
PR => [qw[ 1 0 1 1 1 0 0 0 2 2 1 1 1 2 1 1 2 2 0 1 ]],
PO => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]],
NU => [qw[ 2 0 1 1 1 0 0 0 2 1 1 1 2 1 1 1 2 2 0 1 ]],
AL => [qw[ 2 0 1 1 1 0 0 0 2 2 1 1 2 1 1 1 2 2 0 1 ]],
ID => [qw[ 2 0 1 1 1 0 0 0 2 1 2 2 2 1 1 1 2 2 0 1 ]],
IN => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 1 1 1 2 2 0 1 ]],
HY => [qw[ 2 0 1 1 1 0 0 0 2 2 0 2 2 2 1 1 2 2 0 1 ]],
BA => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 2 0 1 ]],
BB => [qw[ 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 ]],
B2 => [qw[ 2 0 1 1 1 0 0 0 2 2 2 2 2 2 1 1 2 0 0 1 ]],
ZW => [qw[ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 ]],
CM => [qw[ 2 0 1 1 1 0 0 0 2 2 1 1 2 1 1 1 2 2 0 1 ]],
);
# Convert the table above into a hash that we can use for speedier lookups
foreach (keys %BREAK_TABLE) {
my @t = @CLASSES;
$BREAK_TABLE{$_} = { map { shift(@t) => $_ } @{$BREAK_TABLE{$_}} };
}
sub new {
my $pkg = shift;
my $self = { @_ };
$self->{line_length} ||= $columns;
$self->{break_table} ||= \%BREAK_TABLE;
$self->{widthfunc} ||= 1;
bless($self, ref($pkg) || $pkg);
}
# This attempts to identify the on-screen length of a given character.
# For normal displays, you can generally assume the character has a
# length of 1, but some terminals may expand the width of certain
# characters, so that extra space needs to be taken into consideration
# here so the wrapping occurs at the proper place.
sub char_length {
shift if ref($_[0]);
my ($c) = @_;
if ($c eq 'CM' || $c eq 'ZW') {
return 0;
}
return 1;
}
sub lb_class {
my $self = ref($_[0]) ? shift() : self();
my $code = Unicode::UCD::_getcode(ord $_[0]);
my $hex;
if (defined $code) {
$hex = sprintf "%04X", $code;
} else {
carp("unexpected arg \"$_[1]\" to Text::Wrap::lb_class()");
return;
}
return $classified{$hex} if $classified{$hex};
$txt = do "unicore/Lbrk.pl" unless $txt;
if ($txt =~ m/^$hex\t\t(.+)/m) {
print STDERR "< found direct match for $hex = $1 >\n" if $DEBUG > 1;
return $classified{$hex} = $1;
} else {
print STDERR "< no direct match $hex >\n" if $DEBUG > 1;
pos($txt) = 0;
while ($txt =~ m/^([0-9A-F]+)\t([0-9A-F]+)\t(.+)/mg) {
print STDERR "< examining $1 -> $2 >\n" if $DEBUG > 1;
if (hex($1) <= $code && hex($2) >= $code) {
print STDERR "< found range match for $hex = $3 between $1 and $2 >\n" if $DEBUG > 1;
return $classified{$hex} = $3;
}
}
return 'XX';
}
}
# Returns a list of breaking properties for the given text
sub text_properties {
my $self = ref($_[0]) ? shift() : self();
my ($text) = @_;
my @characters = split(//, $text);
my @classifications = map { $self->lb_class($_) } @characters;
class_properties(@classifications);
}
# Returns a list of breaking properties for the provided breaking classes
sub class_properties {
my $self = ref($_[0]) ? shift() : self();
no warnings 'uninitialized';
my @breaks;
my $last_class = $_[0];
$last_class = 'ID' if $last_class eq 'CM'; # broken combining mark
print STDERR "find_breaks: first class=$last_class\n" if $DEBUG;
for (my $i = 1; $i <= $#_; $i++) {
print STDERR "find_breaks: i=$i class=$_[$i] prev=$last_class breaks[i-1]=$breaks[$i-1]\n" if $DEBUG;
$breaks[$i-1] ||= 0;
$_[$i] = 'ID' if $_[$i] eq 'XX'; # we want as few of these as possible!
if ($_[$i] eq 'SA') {
# TODO: Need a classifiation system for complex characters
}
elsif ($_[$i] eq 'CR') {
$breaks[$i] = REQUIRED;
}
elsif ($_[$i] eq 'LF') {
if ($_[$i-1] eq 'CR') {
$breaks[$i-1] = PROHIBITED;
}
$breaks[$i] = REQUIRED;
}
elsif ($_[$i] eq 'BK') {
$breaks[$i] = REQUIRED;
}
elsif ($_[$i] eq 'SP') {
$breaks[$i-1] = PROHIBITED;
next;
}
elsif ($_[$i] eq 'CM') {
if ($_[$i-1] eq 'SP') {
$last_class = 'ID';
if ($i > 1) {
$breaks[$i-2] = $self->{break_table}->{$_[$i-2]}->{ID} ==
DIRECT ? DIRECT : PROHIBITED;
}
}
}
elsif ($last_class ne 'SP') {
if ($breaks[$i-1] != REQUIRED) {
my $this_break = $self->{break_table}->{$last_class}->{$_[$i]};
if ($this_break == INDIRECT) {
$breaks[$i-1] = $_[$i-1] eq 'SP' ? INDIRECT : PROHIBITED;
} else {
# die "internal error: no table mapping between '$last_class' and '$_[$i]'\n"
# unless defined $this_break;
if(defined $this_break)
{
$breaks[$i-1] = $this_break;
}
else
{
$breaks[$i-1] = DIRECT;
}
}
}
}
$last_class = $_[$i];
}
# $breaks[$#breaks] = DIRECT;
push(@breaks, REQUIRED);
print STDERR "find_breaks: returning " . join(":", @breaks) . "\n" if $DEBUG;
return @breaks;
}
# Returns a list of break points in the provided text, based on
# the line length
sub find_breaks {
my $self = ref($_[0]) ? shift() : self();
my $text = shift;
no warnings 'uninitialized'; # since we do a lot of subscript +/- 1 checks
my @characters = split //, $text;
my @classifications = map { $self->lb_class($_) } @characters;
my @lengths = map { $self->char_length($_) } @characters;
my @breaks = $self->class_properties(@classifications);
my @breakpoints;
my $last_start = 0;
my $last_break;
my $last_length;
my $pos = 0;
for (my $i = 0; $i <= $#lengths; $i++) {
print STDERR "[i=$i '$characters[$i]' $classifications[$i] $breaks[$i]] " if $DEBUG;
if ($breaks[$i] == REQUIRED) {
print STDERR "required breakpoint\n" if $DEBUG;
push(@breakpoints, $i+1);
$last_start = $i+1;
$pos = 0;
next;
}
my $c = $pos + $lengths[$i];
if ($c > $self->{line_length}) {
print STDERR "want to break " if $DEBUG;
if (defined $last_break) {
print STDERR "at $last_break\n" if $DEBUG;
push(@breakpoints, $last_break + 1);
$last_start = $last_break + 1;
undef $last_break;
$pos -= $last_length - 1;
print STDERR "[pos now $pos]\n" if $DEBUG;
next;
} elsif (defined $self->{emergency_break} && $c > $self->{emergency_break}) {
print STDERR "NOW\n" if $DEBUG;
push(@breakpoints, $i+1);
$pos = 0;
} else {
print STDERR "but can't" if $DEBUG;
}
}
print STDERR "\n" if $DEBUG;
$last_break = $i if $breaks[$i];
$last_length = $pos if $breaks[$i];
$pos += $lengths[$i];
}
push(@breakpoints, $#lengths) if $breakpoints[$#breakpoints] < $#lengths;
print STDERR "find_breaks: returning breakpoints " . join(":", @breakpoints) . "\n" if $DEBUG;
return @breakpoints;
}
# Returns a list of lines, broken up with find_breaks
sub break_lines {
my $self = ref($_[0]) ? shift() : self();
my $text = shift;
my @breaks = $self->find_breaks($text);
my @lines;
my $last = 0;
foreach (@breaks) {
push(@lines, substr($text, $last, $_-$last));
$last = $_;
}
return @lines;
}
1;
__END__
|