| 1 | # BasicTypesetter.pm -- Module for basic PostScript typesetting | ||
| 2 | # RCS Info : $Id: BasicTypesetter.pm,v 1.18 2003-06-23 15:41:00+02 jv Exp $ | ||
| 3 | # Author : Johan Vromans | ||
| 4 | # Created On : Sun Jun 18 11:40:12 2000 | ||
| 5 | # Last Modified By: Johan Vromans | ||
| 6 | # Last Modified On: Mon Jun 23 15:40:43 2003 | ||
| 7 | # Update Count : 579 | ||
| 8 | # Status : Unknown, Use with caution! | ||
| 9 | |||
| 10 | package PostScript::BasicTypesetter; | ||
| 11 | |||
| 12 | $VERSION = "0.04"; | ||
| 13 | |||
| 14 | use 5.005; | ||
| 15 | use strict; | ||
| 16 | use PostScript::FontMetrics; | ||
| 17 | use PostScript::ISOLatin1Encoding; | ||
| 18 | use Carp; | ||
| 19 | use constant FONTSCALE => 1000; | ||
| 20 | |||
| 21 | sub _error { | ||
| 22 | croak(@_); | ||
| 23 | } | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | PostScript::BasicTypesetter - Module for basic typesetting | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | # Create an object directly from the AFM file. | ||
| 32 | my $ts = new PostScript::BasicTypesetter("/usr/share/metrics/times.afm"); | ||
| 33 | |||
| 34 | # Alternatively, use the Unix PostScript Resources. | ||
| 35 | my $psres = new PostScript::Resources; | ||
| 36 | my $m = new PostScript::FontMetrics($psres->FontAFM("Times-Roman")); | ||
| 37 | # The metrics object can now be shared between typesetters. | ||
| 38 | my $ts = new PostScript::BasicTypesetter($m); | ||
| 39 | |||
| 40 | # Re-encode the font to use ISO Latin-1 encoding. | ||
| 41 | # The resultant font will be named Times-Romand-Latin1. | ||
| 42 | $ts->reencode("ISOLatin1Encoding", "Latin1"); | ||
| 43 | |||
| 44 | # Set the current font size to 10 points. | ||
| 45 | $ts->fontsize(10); | ||
| 46 | # Set the lineskip to 12 points. | ||
| 47 | $ts->lineskip(12); | ||
| 48 | |||
| 49 | # Add to the PostScript preamble: | ||
| 50 | print $ts->ps_reencodesub; | ||
| 51 | print $ts->ps_preamble; | ||
| 52 | |||
| 53 | # Add to the PostScript Setup: | ||
| 54 | print $ts->ps_reencode; | ||
| 55 | |||
| 56 | # To typeset a box of text at $x, $y, width $w: | ||
| 57 | print $ts->ps_textbox($x,$y,$w,$str); | ||
| 58 | |||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | PostScript::BasicTypesetter is an experimental module to facilitate | ||
| 63 | PostScript based typesetting. Its principal reason for existance is | ||
| 64 | that it properly uses font metrics for precise typesetting and | ||
| 65 | kerning. | ||
| 66 | |||
| 67 | A BasicTypesetter object maintains font information (name, encoding, | ||
| 68 | and size) that can be used to typeset texts. Every operation invoked | ||
| 69 | on a typesetter object is executed w.r.t. the current values for the | ||
| 70 | font, encoding and size. However, once the PostScript preamble has | ||
| 71 | been constructed, the font name and encoding should not be changed | ||
| 72 | anymore. | ||
| 73 | |||
| 74 | =head1 CONSTRUCTOR | ||
| 75 | |||
| 76 | =head2 new | ||
| 77 | |||
| 78 | Example: | ||
| 79 | |||
| 80 | $ts = new PostScript::BasicTypesetter ($arg); | ||
| 81 | |||
| 82 | This constructs a new typesetter object. | ||
| 83 | |||
| 84 | The argument may be a PostScript::FontMetrics object, or the name of a | ||
| 85 | valid font metrics file. | ||
| 86 | |||
| 87 | If the argument refers to a FontMetrics object, this object will be | ||
| 88 | associated with the Typesetter object, and it can be shared between | ||
| 89 | Typesetters. If it is a file name, a private FontMetrics object will | ||
| 90 | be created. | ||
| 91 | |||
| 92 | The new typesetter gets a default value of C<10> for the font size, | ||
| 93 | and C<12> for the lineskip. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | my $def_fontsize = 10; | ||
| 98 | my $def_lineskip = 12; | ||
| 99 | my $def_hyphens = ''; | ||
| 100 | |||
| 101 | sub new { | ||
| 102 | my $class = shift; | ||
| 103 | my $metrics = shift; | ||
| 104 | my $self = {}; | ||
| 105 | |||
| 106 | my (%atts) = (verbose => 0, trace => 0, debug => 0, | ||
| 107 | @_); | ||
| 108 | $self->{debug} =$atts{debug}; | ||
| 109 | $self->{trace} = $self->{debug} || $atts{trace}; | ||
| 110 | $self->{verbose} = $self->{trace} || $atts{verbose}; | ||
| 111 | |||
| 112 | # Need either a PostScript::FontMetrics object, or a file name. | ||
| 113 | if ( UNIVERSAL::isa($metrics, 'PostScript::FontMetrics') ) { | ||
| 114 | $self->{metrics} = $metrics; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $self->{metrics} = new PostScript::FontMetrics ($metrics, %atts); | ||
| 118 | } | ||
| 119 | bless $self, $class; | ||
| 120 | $self->{fontsize} = $def_fontsize; | ||
| 121 | $self->{lineskip} = $def_lineskip; | ||
| 122 | $self->{hyphens} = $def_hyphens; | ||
| 123 | $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 clone | ||
| 127 | |||
| 128 | Example: | ||
| 129 | |||
| 130 | $newts = $ts->clone; # identical | ||
| 131 | $ts12 = $ts->clone(12); # identical, but font size 12 | ||
| 132 | |||
| 133 | Creates a new typesetter with identical contents and shared metrics. | ||
| 134 | If additional arguments are present, these are passed to the | ||
| 135 | C<fontsize> method. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub clone { | ||
| 140 | my $self = shift; | ||
| 141 | $self = bless { %$self }, ref($self); | ||
| 142 | $self->fontsize(@_) if @_; | ||
| 143 | $self; | ||
| 144 | } | ||
| 145 | |||
| 146 | =head1 INSTANCE METHODS | ||
| 147 | |||
| 148 | =head2 fontname | ||
| 149 | |||
| 150 | Example: | ||
| 151 | |||
| 152 | $name = $ts->fontname; | ||
| 153 | |||
| 154 | This routine returns the name of the font, which may differ from the | ||
| 155 | real font name if the font has been reencoded. For example, | ||
| 156 | re-encoding Times-Roman (the real font name) may result in a font that | ||
| 157 | will be known as Times-Roman-Latin1. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub fontname { | ||
| 162 | my ($self) = @_; | ||
| 163 | |||
| 164 | # If the font has been reencoded, return the new font name. | ||
| 165 | $self->metrics->{tp_encodedfontname} || $self->metrics->FontName; | ||
| 166 | } | ||
| 167 | |||
| 168 | =head2 real_fontname | ||
| 169 | |||
| 170 | Example: | ||
| 171 | |||
| 172 | $name = $ts->real_fontname; | ||
| 173 | |||
| 174 | This routine returns the real name of the font. | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub real_fontname { | ||
| 179 | my ($self) = @_; | ||
| 180 | |||
| 181 | $self->metrics->FontName; | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 metrics | ||
| 185 | |||
| 186 | $metrics = $ts->metrics; | ||
| 187 | |||
| 188 | This routine returns provides access to the associated | ||
| 189 | PostScript::FontMetrics object. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub metrics { | ||
| 194 | my ($self) = @_; | ||
| 195 | |||
| 196 | $self->{metrics}; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 reencode | ||
| 200 | |||
| 201 | Example: | ||
| 202 | |||
| 203 | $ts->reeencode ($enc, $tag, $changes); | ||
| 204 | |||
| 205 | This routine re-encodes the font associated with this typesetter | ||
| 206 | according to the specified encoding, and optionally modifies the | ||
| 207 | resultant encoding according to the changes vector. The new font will | ||
| 208 | get the name of the original font, with C<->I<tag> appended. | ||
| 209 | |||
| 210 | C<$baseenc> must be either C<"StandardEncoding"> (default), | ||
| 211 | C<"ISOLatin1Encoding">, or C<"ISOLatin9Encoding">. | ||
| 212 | |||
| 213 | C<$changes>, if specified, must be a reference to a hash. For each key | ||
| 214 | of the hash, its ordinal value in the resultant encoding will be set | ||
| 215 | to its value, which must be a valid glyph name. | ||
| 216 | |||
| 217 | For example, to re-encode a font to ISO-Latin1 and add the glyph | ||
| 218 | C<ellipsis> to location 0200 (octal), the following call can be used: | ||
| 219 | |||
| 220 | $ts->reencode ("ISOLatin1Encoding", "Latin1", {"\200" => "ellipsis"}); | ||
| 221 | |||
| 222 | B<WARNING:> C<reencode> affects the FontMetrics object that is | ||
| 223 | associated with this Typesetter. When FontMetrics objects are shared | ||
| 224 | between Typesetters, Calling C<reencode> on one of the Typesetters will | ||
| 225 | affect the other Typesetters as well. This is deliberate. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | # Reencode a font to a specific base, with small modifications. | ||
| 230 | sub reencode { | ||
| 231 | my ($self, $base, $tag, $vec) = @_; | ||
| 232 | |||
| 233 | my $baseenc = $base || "StandardEncoding"; | ||
| 234 | |||
| 235 | # Check encoding, and get a ref to the array. | ||
| 236 | if ( $baseenc eq "StandardEncoding" ) { | ||
| 237 | require PostScript::StandardEncoding; | ||
| 238 | $base = PostScript::StandardEncoding->array; | ||
| 239 | } | ||
| 240 | elsif ( $baseenc eq "ISOLatin1Encoding" ) { | ||
| 241 | $base = PostScript::ISOLatin1Encoding->array; | ||
| 242 | } | ||
| 243 | elsif ( $baseenc eq "ISOLatin9Encoding" ) { | ||
| 244 | require PostScript::ISOLatin9Encoding; | ||
| 245 | $base = PostScript::ISOLatin9Encoding->array; | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | _error ("Invalid encoding: $baseenc"); | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | $self->metrics->{tp_reencodebase} = $baseenc; | ||
| 252 | |||
| 253 | # Reencode according to the vector.. | ||
| 254 | if ( $vec ) { | ||
| 255 | $base = [@$base]; # copy | ||
| 256 | my ($k, $v); | ||
| 257 | while ( ($k,$v) = each (%$vec) ) { | ||
| 258 | $base->[ord($k)] = $v; | ||
| 259 | } | ||
| 260 | $self->metrics->{tp_reencodevector} = { %$vec }; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | undef $self->metrics->{tp_reencodevector}; | ||
| 264 | } | ||
| 265 | |||
| 266 | # Set the new encoding. | ||
| 267 | $self->metrics->setEncoding ($base); | ||
| 268 | |||
| 269 | # Form a new name for the font. | ||
| 270 | $self->metrics->{tp_encodedfontname} = | ||
| 271 | $self->{metrics}->FontName . "-" . $tag; | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 fontsize | ||
| 275 | |||
| 276 | Example: | ||
| 277 | |||
| 278 | $ts->fontsize(12, 15); | ||
| 279 | $size = $ts->fontsize; | ||
| 280 | |||
| 281 | Sets or gets the current font size. When setting, the (optional) | ||
| 282 | second argument can be used to simultaneously set the lineskip. | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub fontsize { | ||
| 287 | my ($self, $size, $skip) = @_; | ||
| 288 | $self->{fontsize} = $size if $size; | ||
| 289 | $self->{lineskip} = $skip if $skip; | ||
| 290 | $self->{fontsize}; | ||
| 291 | } | ||
| 292 | |||
| 293 | =head2 lineskip | ||
| 294 | |||
| 295 | Example: | ||
| 296 | |||
| 297 | $ts->lineskip(15); | ||
| 298 | $skip = $ts->lineskip; | ||
| 299 | |||
| 300 | Sets or gets the current lineskip. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | sub lineskip { | ||
| 305 | my ($self, $skip) = @_; | ||
| 306 | $self->{lineskip} = $skip if $skip; | ||
| 307 | $self->{lineskip}; | ||
| 308 | } | ||
| 309 | |||
| 310 | =head2 hyphens | ||
| 311 | |||
| 312 | Example: | ||
| 313 | |||
| 314 | $ts->hyphens('-/'); | ||
| 315 | $hyphens = $ts->hyphens; | ||
| 316 | |||
| 317 | Sets or gets the current string of characters considered to be hyphens | ||
| 318 | (points words can be split on a line break). Disabled by default. | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | sub hyphens { | ||
| 323 | my ($self, $hyphens) = @_; | ||
| 324 | $self->{hyphens} = $hyphens if defined $hyphens; | ||
| 325 | $self->{hyphens}; | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 linebreak | ||
| 329 | |||
| 330 | Example: | ||
| 331 | |||
| 332 | $ts->linebreak(\&break); | ||
| 333 | |||
| 334 | Sets or gets the code to be executed when C<ps_textbox> advances due | ||
| 335 | to a line break. The subroutine is called as a method with a reference | ||
| 336 | to the current vertical position and should adjust the value. The | ||
| 337 | routine should return PostScript instructions that are necessary for | ||
| 338 | the line break, if any. | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | sub linebreak { | ||
| 343 | my ($self, $skip) = @_; | ||
| 344 | $self->{linebreak} = $skip if $skip; | ||
| 345 | $self->{linebreak}; | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 color | ||
| 349 | |||
| 350 | Example: | ||
| 351 | |||
| 352 | $ts->color([1,0,0]); # RGB vector | ||
| 353 | $color = $ts->color; | ||
| 354 | |||
| 355 | Sets or gets the current color. The argument must be either a | ||
| 356 | 3-element vector for RGB color coordinates, or an 4-element vector for | ||
| 357 | CMYK coordinates. In either case, each of the coordinates must be a number | ||
| 358 | between 0 and 1, inclusive. | ||
| 359 | |||
| 360 | IMPORTANT: There's no such thing as a default color. Typesetters that | ||
| 361 | do not have a color set will print in the current color. Use | ||
| 362 | C<ps_setcolor>, described below, to explicitly control the color of | ||
| 363 | the output. | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub color { | ||
| 368 | my ($self, $color) = @_; | ||
| 369 | if ( $color ) { | ||
| 370 | croak ("color: Color must be a 3 or 4 element vector") | ||
| 371 | unless ref($color) && UNIVERSAL::isa($color,"ARRAY") | ||
| 372 | && (@$color == 3 || @$color == 4); | ||
| 373 | } | ||
| 374 | $self->{color} = $color if $color; | ||
| 375 | $self->{color}; | ||
| 376 | } | ||
| 377 | |||
| 378 | =head2 stringwidth | ||
| 379 | |||
| 380 | Example: | ||
| 381 | |||
| 382 | $width = $ts->stringwidth($str); | ||
| 383 | |||
| 384 | Returns the width of the string for the current font and size, | ||
| 385 | with kerning information applied. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub stringwidth { | ||
| 390 | my $self = shift; | ||
| 391 | |||
| 392 | my ($str) = @_; | ||
| 393 | my $size = $self->{fontsize}; | ||
| 394 | $self->metrics->kstringwidth($str)*$size/FONTSCALE; | ||
| 395 | } | ||
| 396 | |||
| 397 | # Internal helper w/o kerning. | ||
| 398 | |||
| 399 | sub _stringwidth { | ||
| 400 | my $self = shift; | ||
| 401 | |||
| 402 | my ($str) = @_; | ||
| 403 | my $size = $self->{fontsize}; | ||
| 404 | $self->metrics->stringwidth($str)*$size/FONTSCALE; | ||
| 405 | } | ||
| 406 | |||
| 407 | =head2 tjvector | ||
| 408 | |||
| 409 | Example: | ||
| 410 | |||
| 411 | $vec = $ts->tjvector($str); | ||
| 412 | |||
| 413 | Returns the typesetting vector for the string, with kerning | ||
| 414 | information applied. This vector can be passed to the C<ps_tj> | ||
| 415 | method. | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | sub tjvector { | ||
| 420 | my $self = shift; | ||
| 421 | |||
| 422 | $self->metrics->kstring(@_); | ||
| 423 | } | ||
| 424 | |||
| 425 | =head2 textwidth | ||
| 426 | |||
| 427 | Example: | ||
| 428 | |||
| 429 | $width = $ts->textwidth; | ||
| 430 | |||
| 431 | Returns the width of the last textbox set by this typesetter. | ||
| 432 | |||
| 433 | =cut | ||
| 434 | |||
| 435 | sub textwidth { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | my ($str) = @_; | ||
| 439 | $self->{textwidth}; | ||
| 440 | } | ||
| 441 | |||
| 442 | =head2 char | ||
| 443 | |||
| 444 | Example: | ||
| 445 | |||
| 446 | $char = $ts->char("quotedblleft"); | ||
| 447 | |||
| 448 | Returns a one-character string that will render as the named glyph in | ||
| 449 | the current encoding, or C<undef> if this glyph is currently not | ||
| 450 | encoded. | ||
| 451 | |||
| 452 | This is a convenience method that calls the C<char> method of the | ||
| 453 | associated FontMetrics object. | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | sub char { | ||
| 458 | my ($self, $glyph) = @_; | ||
| 459 | $self->metrics->char ($glyph); | ||
| 460 | } | ||
| 461 | |||
| 462 | ################ PostScript code builders ################ | ||
| 463 | |||
| 464 | # All ps_ routines return a printable PostScript string. | ||
| 465 | |||
| 466 | =head2 ps_preamble | ||
| 467 | |||
| 468 | Example: | ||
| 469 | |||
| 470 | print $ts->ps_preamble; | ||
| 471 | |||
| 472 | Produces the PostScript code for the preamble. | ||
| 473 | |||
| 474 | To be used while constructing the PostScript preamble. | ||
| 475 | |||
| 476 | =cut | ||
| 477 | |||
| 478 | sub ps_preamble { | ||
| 479 | my $self = shift; | ||
| 480 | my $ret = <<EOD; | ||
| 481 | % TJ operator to print typesetinfo vectors. | ||
| 482 | /TJ { | ||
| 483 | { dup type /stringtype eq { show } { 0 rmoveto } ifelse } | ||
| 484 | forall | ||
| 485 | } bind def | ||
| 486 | EOD | ||
| 487 | if ( defined(&PostScript::ISOLatin9Encoding::ps_vector) ) { | ||
| 488 | $ret .= PostScript::ISOLatin9Encoding::ps_vector(); | ||
| 489 | } | ||
| 490 | $ret; | ||
| 491 | } | ||
| 492 | |||
| 493 | =head2 ps_reencodesub | ||
| 494 | |||
| 495 | Example: | ||
| 496 | |||
| 497 | print $ts->ps_reencodesub (name => "ReEncode"); | ||
| 498 | |||
| 499 | Produces the PostScript code to define a PostScript routine to reencode | ||
| 500 | the fonts. | ||
| 501 | |||
| 502 | To be used while constructing the PostScript preamble. | ||
| 503 | |||
| 504 | Attributes: | ||
| 505 | |||
| 506 | =over | ||
| 507 | |||
| 508 | =item name | ||
| 509 | |||
| 510 | The name prefix of the subroutine. If the reencoding vector is | ||
| 511 | embedded (see below), this is the actual name for the routine. | ||
| 512 | Otherwise, the actual name for the routine is I<prefix>C<Sub>, and the | ||
| 513 | vector will be named I<prefix>C<Vec>. | ||
| 514 | |||
| 515 | =item base | ||
| 516 | |||
| 517 | The base encoding. This should be either C<StandardEncoding>, | ||
| 518 | C<ISOLatin1Encoding>m or C<ISOLatin9Encoding>. It defaults to the | ||
| 519 | value supplied with a preceding C<reencode> call, or | ||
| 520 | C<StandardEncoding> if no such call has been issued. | ||
| 521 | |||
| 522 | =item vec | ||
| 523 | |||
| 524 | If this is C<"embedded"> (default), the reencoding vector will be part | ||
| 525 | of the subroutine. Otherwise it will be defined separately. | ||
| 526 | |||
| 527 | =back | ||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | sub ps_reencodesub { | ||
| 532 | my ($self) = shift; | ||
| 533 | |||
| 534 | my %atts = ( name => "ReEncode", | ||
| 535 | base => $self->metrics->{tp_reencodebase} || "AdobeStandardEncoding", | ||
| 536 | vec => "embedded", | ||
| 537 | @_); | ||
| 538 | |||
| 539 | my $base = $atts{base}; | ||
| 540 | my $name = $atts{name}; | ||
| 541 | |||
| 542 | my $ret = ""; | ||
| 543 | |||
| 544 | if ( $atts{vec} eq "embedded" ) { | ||
| 545 | $ret .= "/${name} {\n /newcodesandnames [\n"; | ||
| 546 | my ($k, $v); | ||
| 547 | while ( ($k,$v) = each (%{$self->metrics->{tp_reencodevector}}) ) { | ||
| 548 | $ret .= sprintf (" 8#%03o /%s\n", ord($k), $v); | ||
| 549 | } | ||
| 550 | $ret .= " ] def\n"; | ||
| 551 | } | ||
| 552 | else { | ||
| 553 | $ret .= "/${name}Sub {\n /newcodesandnames exch def\n"; | ||
| 554 | } | ||
| 555 | $ret .= <<EOD; | ||
| 556 | /newfontname exch def | ||
| 557 | /basefontname exch def | ||
| 558 | |||
| 559 | /basefontdict basefontname findfont def | ||
| 560 | /newfont basefontdict maxlength dict def | ||
| 561 | basefontdict | ||
| 562 | { exch dup /FID ne | ||
| 563 | { dup /Encoding eq | ||
| 564 | { exch pop $base dup length array copy | ||
| 565 | newfont 3 1 roll put | ||
| 566 | } | ||
| 567 | { exch newfont 3 1 roll put } | ||
| 568 | ifelse | ||
| 569 | } | ||
| 570 | { pop pop } | ||
| 571 | ifelse | ||
| 572 | } | ||
| 573 | forall | ||
| 574 | |||
| 575 | newfont /FontName newfontname put | ||
| 576 | newcodesandnames aload pop | ||
| 577 | newcodesandnames length 2 idiv | ||
| 578 | { newfont /Encoding get 3 1 roll put } | ||
| 579 | repeat | ||
| 580 | |||
| 581 | newfontname newfont definefont pop | ||
| 582 | } def | ||
| 583 | EOD | ||
| 584 | |||
| 585 | if ( $atts{vec} ne "embedded" ) { | ||
| 586 | $ret .= "/${name}Vec [\n"; | ||
| 587 | my ($k, $v); | ||
| 588 | while ( ($k,$v) = each (%{$self->metrics->{tp_reencodevector}}) ) { | ||
| 589 | $ret .= sprintf (" 8#%03o /%s\n", ord($k), $v); | ||
| 590 | } | ||
| 591 | $ret .= "] def\n"; | ||
| 592 | } | ||
| 593 | |||
| 594 | $ret; | ||
| 595 | } | ||
| 596 | |||
| 597 | =head2 ps_reencode | ||
| 598 | |||
| 599 | Example: | ||
| 600 | |||
| 601 | print $ts->ps_reencode (name => "ReEncode"); | ||
| 602 | |||
| 603 | Produces the PostScript code to reencode a font. | ||
| 604 | Nothing is produced if the font has not been re-encoded. | ||
| 605 | |||
| 606 | To be used while constructiong the PostScript C<Setup> section. | ||
| 607 | |||
| 608 | Attributes: | ||
| 609 | |||
| 610 | =over | ||
| 611 | |||
| 612 | =item name | ||
| 613 | |||
| 614 | The name prefix of the subroutine. If the reencoding vector is | ||
| 615 | embedded (see below), this is the actual name for the routine. | ||
| 616 | Otherwise, the actual name for the routine is I<prefix>C<Sub>, and the | ||
| 617 | vector will be named I<prefix>C<Vec>. | ||
| 618 | |||
| 619 | =item vec | ||
| 620 | |||
| 621 | If this is C<"embedded"> (default), the reencoding vector will be | ||
| 622 | assumed part of the subroutine. Otherwise it will be specified | ||
| 623 | separately. | ||
| 624 | |||
| 625 | =back | ||
| 626 | |||
| 627 | =cut | ||
| 628 | |||
| 629 | sub ps_reencode { | ||
| 630 | my $self = shift; | ||
| 631 | my %atts = ( name => "ReEncode", | ||
| 632 | vec => "embedded", | ||
| 633 | @_); | ||
| 634 | |||
| 635 | return "" if $self->{metrics}->FontName eq $self->fontname; | ||
| 636 | |||
| 637 | my $name = $atts{name}; | ||
| 638 | "/" . $self->{metrics}->FontName . " " . | ||
| 639 | "/" . $self->fontname . " " . | ||
| 640 | ($atts{vec} eq "embedded" ? "$name" : | ||
| 641 | ($name . "Vec " . $name . "Sub)")) . "\n"; | ||
| 642 | } | ||
| 643 | |||
| 644 | |||
| 645 | =head2 ps_str | ||
| 646 | |||
| 647 | Example: | ||
| 648 | |||
| 649 | print $ts->ps_str ($str); | ||
| 650 | |||
| 651 | Produces the PostScript representation for the given string. | ||
| 652 | |||
| 653 | =cut | ||
| 654 | |||
| 655 | sub ps_str { | ||
| 656 | my $self = shift; | ||
| 657 | |||
| 658 | # Externalize a PostScript string. | ||
| 659 | local ($_) = @_; | ||
| 660 | s/([\\\200-\377()])/sprintf("\\%03o",ord($1))/eg; | ||
| 661 | '(' . $_ . ')'; | ||
| 662 | } | ||
| 663 | |||
| 664 | =head2 ps_setfont | ||
| 665 | |||
| 666 | Example: | ||
| 667 | |||
| 668 | print $ts->ps_setfont; | ||
| 669 | |||
| 670 | Produces the PostScript code to designate the current font, size and | ||
| 671 | color for PostScript. | ||
| 672 | |||
| 673 | This method keeps track of the settings, and will not produce | ||
| 674 | anything if the current settings are already as requested. Hence use | ||
| 675 | liberally. | ||
| 676 | |||
| 677 | Call with an explicit C<undef> argument to flush the cache. This is | ||
| 678 | only necessary when the PostScript code needs to maintain graphics | ||
| 679 | state explicitly. | ||
| 680 | |||
| 681 | =cut | ||
| 682 | |||
| 683 | my $ps_curfont; | ||
| 684 | |||
| 685 | sub ps_setfont { | ||
| 686 | my $self = shift; | ||
| 687 | |||
| 688 | my $ret = ''; | ||
| 689 | my $size = $self->{fontsize}; | ||
| 690 | croak ("ps_setfont: Font size not set") unless $size; | ||
| 691 | if ( @_ && !defined shift ) { | ||
| 692 | undef $ps_curfont; | ||
| 693 | $self->ps_setcolor (undef); | ||
| 694 | return $ret; | ||
| 695 | } | ||
| 696 | unless ( $ps_curfont && $ps_curfont eq "$size $self->fontname" ) { | ||
| 697 | $ret .= sprintf ("/%s findfont %.3g scalefont setfont\n", | ||
| 698 | $self->fontname, $size); | ||
| 699 | $ps_curfont = "$size $self->fontname"; | ||
| 700 | } | ||
| 701 | $ret .= $self->ps_setcolor ($self->{color}) | ||
| 702 | if $self->{color}; | ||
| 703 | |||
| 704 | $ret; | ||
| 705 | } | ||
| 706 | |||
| 707 | =head2 ps_setcolor | ||
| 708 | |||
| 709 | Example: | ||
| 710 | |||
| 711 | print $ts->ps_setcolor([0,0,0]); | ||
| 712 | |||
| 713 | Produces the PostScript code to set the current PostScript color. | ||
| 714 | If no arguments are passed, it sets the value for this typesetter. | ||
| 715 | |||
| 716 | This method keeps track of the settings, and will not produce | ||
| 717 | anything if the current settings are already as requested. Hence use | ||
| 718 | liberally. | ||
| 719 | |||
| 720 | Call with an explicit C<undef> argument to flush the cache. This is | ||
| 721 | only necessary when the PostScript code needs to maintain graphics | ||
| 722 | state explicitly. | ||
| 723 | |||
| 724 | =cut | ||
| 725 | |||
| 726 | my $ps_curcol; | ||
| 727 | |||
| 728 | sub ps_setcolor { | ||
| 729 | my $self = shift; | ||
| 730 | my $color = $self->{color}; | ||
| 731 | my $ret = ''; | ||
| 732 | if ( @_ ) { | ||
| 733 | $color = shift; | ||
| 734 | if ( defined $color ) { | ||
| 735 | croak ("color: Color must be a 3 or 4 element vector") | ||
| 736 | unless ref($color) && UNIVERSAL::isa($color,"ARRAY") | ||
| 737 | && (@$color == 3 || @$color == 4); | ||
| 738 | } | ||
| 739 | else { | ||
| 740 | undef $ps_curcol; | ||
| 741 | return $ret; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | return $ret unless $color; | ||
| 745 | unless ( $ps_curcol | ||
| 746 | && join(" ",@$ps_curcol) eq join(" ",$color) ) { | ||
| 747 | $ps_curcol = $color; | ||
| 748 | if ( @$color == 3 ) { | ||
| 749 | $ret .= sprintf ("%.3f %.3f %.3f setrgbcolor\n", @$color); | ||
| 750 | } | ||
| 751 | else { | ||
| 752 | $ret .= sprintf ("%.3f %.3f %.3f %.3f setcmykcolor\n", | ||
| 753 | @$color); | ||
| 754 | } | ||
| 755 | } | ||
| 756 | $ret; | ||
| 757 | } | ||
| 758 | |||
| 759 | =head2 ps_tj | ||
| 760 | |||
| 761 | Example: | ||
| 762 | |||
| 763 | print $ts->ps_tj ($tj); | ||
| 764 | |||
| 765 | Produces the PostScript code to print the text at the current position. | ||
| 766 | The argument to this function must be the result of a call to C<tjvector>. | ||
| 767 | |||
| 768 | =cut | ||
| 769 | |||
| 770 | # Print a typesetting vector. Use TJ definition. | ||
| 771 | sub ps_tj { | ||
| 772 | my $self = shift; | ||
| 773 | local ($_); | ||
| 774 | |||
| 775 | my ($t) = @_; | ||
| 776 | my $ret = ''; | ||
| 777 | my $size = $self->{fontsize}; | ||
| 778 | croak ("ps_tj: Font size not set") unless $size; | ||
| 779 | my $scale = $size/FONTSCALE; | ||
| 780 | $ret .= "["; | ||
| 781 | my $l = 1; | ||
| 782 | foreach ( @$t ) { | ||
| 783 | unless ( /^\(/ ) { | ||
| 784 | $_ = sprintf("%.4f", $_*$scale); | ||
| 785 | s/0+$//; | ||
| 786 | } | ||
| 787 | if ( ($l += length) >= 80 ) { | ||
| 788 | $ret .= "\n "; | ||
| 789 | $l = 1 + length; | ||
| 790 | } | ||
| 791 | $ret .= $_; | ||
| 792 | } | ||
| 793 | $ret .= "] TJ\n"; | ||
| 794 | $ret; | ||
| 795 | } | ||
| 796 | |||
| 797 | =head2 ps_linebreak | ||
| 798 | |||
| 799 | Example: | ||
| 800 | |||
| 801 | print $ts->ps_linebreak (\$y); | ||
| 802 | |||
| 803 | Produces the PostScript code to advance to the next logical line, | ||
| 804 | advancing the value of C<$y>. The default operation is to decrement | ||
| 805 | C<$y> with the value of C<$ts->lineskip>, but C<$tr->linebreak> may be | ||
| 806 | called to install a different linebreak handler. | ||
| 807 | |||
| 808 | =cut | ||
| 809 | |||
| 810 | sub ps_linebreak { | ||
| 811 | my ($self, $yref) = @_; | ||
| 812 | my $code = $self->{linebreak}; | ||
| 813 | return $code->($self, $yref) if $code; | ||
| 814 | $$yref -= $self->{lineskip}; | ||
| 815 | ''; | ||
| 816 | } | ||
| 817 | |||
| 818 | =head2 ps_textbox | ||
| 819 | |||
| 820 | Example: | ||
| 821 | |||
| 822 | print $ts->ps_textbox ($x, $xi, $y, $w, $str, $align); | ||
| 823 | |||
| 824 | Produces the PostScript code to typeset a string, or a set of strings. | ||
| 825 | |||
| 826 | The string will be printed starting at base postion C<$x> and C<$y>. | ||
| 827 | If the string exceeds the width C<$w> a line wrap will occur and the | ||
| 828 | C<ps_linebreak> method of the typesetter is called. | ||
| 829 | |||
| 830 | The first line will be indented with C<$xi>. | ||
| 831 | |||
| 832 | C<ps_textbox> will take care of font changes and color settings, but | ||
| 833 | will not affect the global settings. | ||
| 834 | |||
| 835 | If C<$str> is a reference to an array, this array may contain a mix of | ||
| 836 | strings, PostScript::BasicTypesetter objects, and array references | ||
| 837 | (recursive). Each string is typeset according to the current | ||
| 838 | typesetter; a typesetter object changes the current typesetter for the | ||
| 839 | rest of the array. However, the linebreak handler of the initial | ||
| 840 | typesetter is used for linewraps, regardless the typesetter currently | ||
| 841 | in control. | ||
| 842 | |||
| 843 | C<$xi> and/or C<$y> may be references to the actual values. In this | ||
| 844 | case, the corresponding values will be updated to reflect the value | ||
| 845 | upon completion. In other words, the resulting C<$xi> value will | ||
| 846 | reflect the x-position directly after the last character of the last | ||
| 847 | line. The C<$y> value will reflect the baseline of the last line that | ||
| 848 | was typeset. Using references to the actual values, series of calls to | ||
| 849 | C<ps_textbox> can be chained: each call will continue exactly where | ||
| 850 | the preceding call left off. This is, of course, only useful with | ||
| 851 | flush left alignment. | ||
| 852 | |||
| 853 | The method C<textwidth> will return the actual width of the text that | ||
| 854 | was set. Note that this may be larger than the specified width of the | ||
| 855 | text box, when the text contains unbreakable items larger than the | ||
| 856 | desired width. | ||
| 857 | |||
| 858 | Values for C<$align> are C<"l"> (default): flush left, C<"r">: flush | ||
| 859 | right, C<"c">: centered, C<"j">: justified. | ||
| 860 | |||
| 861 | B<NOTE:> The string(s) should I<not> contain tabs and newlines, since | ||
| 862 | these may get a different meaning in the future. Currently, tabs and | ||
| 863 | newlines are treated as whitespace (and mostly ignored). | ||
| 864 | |||
| 865 | =cut | ||
| 866 | |||
| 867 | sub ps_textbox { | ||
| 868 | my ($self, $x, $xxi, $yy, $width, $t, $align) = @_; | ||
| 869 | |||
| 870 | # Default is flush left. | ||
| 871 | $align = (defined $align) ? lc($align) : 'l'; | ||
| 872 | croak ("ps_textbox: Unhandled alignment '$align'") | ||
| 873 | unless $align =~ /^[lrjc]$/; | ||
| 874 | |||
| 875 | return _ps_simpletextbox ($self, $x, $xxi, $yy, $width, $t, $align) | ||
| 876 | unless ref ($t); | ||
| 877 | |||
| 878 | return _ps_textbox ($self, $x, $xxi, $yy, $width, $t, $align); | ||
| 879 | } | ||
| 880 | |||
| 881 | # Internal helper routine. This is for the multi-font case. | ||
| 882 | sub _ps_textbox { | ||
| 883 | my ($self, $x, $xxi, $yy, $width, $t, $align) = @_; | ||
| 884 | |||
| 885 | # Default is flush left. | ||
| 886 | $align = (defined $align) ? lc($align) : 'l'; | ||
| 887 | croak ("ps_textbox: Unhandled alignment '$align'") | ||
| 888 | unless $align =~ /^[lrjc]$/; | ||
| 889 | |||
| 890 | my $cur = $self; | ||
| 891 | my $cur0 = $cur; | ||
| 892 | my $cur00 = $cur; | ||
| 893 | |||
| 894 | # Deref arguments, if needed. | ||
| 895 | my $xi = ref($xxi) ? $$xxi : $xxi; | ||
| 896 | my $y = ref($yy) ? $$yy : $yy; | ||
| 897 | |||
| 898 | my $scale; # 1000/fontsize | ||
| 899 | my $wspace; # width of a space | ||
| 900 | my $wd = $xi; # accumulated width | ||
| 901 | my $ret = ''; # accumulated output | ||
| 902 | my $lskip = $cur0->lineskip; # line skip (fixed) | ||
| 903 | my $lbreak = $cur0->{linebreak}; | ||
| 904 | my $maxwidth = 0; # max width of textbox | ||
| 905 | |||
| 906 | # Setup global values for this font. | ||
| 907 | my $switch_font = sub { | ||
| 908 | $scale = FONTSCALE/$cur->{fontsize}; | ||
| 909 | $wspace = $cur->_stringwidth(" "); | ||
| 910 | croak ("ps_textbox: [".$cur->metrics->FontName."]: missing space") | ||
| 911 | unless $wspace > 0; | ||
| 912 | }; | ||
| 913 | |||
| 914 | my @res; | ||
| 915 | my $overflow; # we did a line wrap | ||
| 916 | |||
| 917 | # This is the actual typesetting routine. | ||
| 918 | my $flush = sub { | ||
| 919 | my $did = 0; | ||
| 920 | $cur = $cur0; | ||
| 921 | $switch_font->(); | ||
| 922 | |||
| 923 | $wd = $xi; | ||
| 924 | my $t = [$cur]; # current ts vector | ||
| 925 | my $tt = []; # accumulated vectors | ||
| 926 | my $nsp = 0; # available space for stretching | ||
| 927 | |||
| 928 | # Discard possible leading/trailing space. | ||
| 929 | shift(@res) while $overflow && !ref($res[0]); | ||
| 930 | |||
| 931 | my $ts = $res[-1]; | ||
| 932 | if ( ref($ts) && UNIVERSAL::isa($ts,PostScript::BasicTypesetter::) ) { | ||
| 933 | pop(@res); | ||
| 934 | } | ||
| 935 | else { | ||
| 936 | undef $ts; | ||
| 937 | } | ||
| 938 | pop(@res) until ref($res[-1]) || @res == 0; | ||
| 939 | push (@res, $ts) if $ts; | ||
| 940 | |||
| 941 | foreach ( @res ) { | ||
| 942 | if ( ref($_) ) { | ||
| 943 | if ( UNIVERSAL::isa($_,PostScript::BasicTypesetter::) ) { | ||
| 944 | push (@$tt, $t) if @$t; | ||
| 945 | $t = [$cur = $_]; | ||
| 946 | $switch_font->(); | ||
| 947 | next; | ||
| 948 | } | ||
| 949 | # width/kstring pair -> collect the string and tally the width. | ||
| 950 | $wd += shift(@$_); | ||
| 951 | push (@$t, $_); # push as ref | ||
| 952 | } | ||
| 953 | else { | ||
| 954 | # Width. Collect (scaled). | ||
| 955 | push (@$t, $_*$scale); | ||
| 956 | $wd += $_; | ||
| 957 | # Tally available space. | ||
| 958 | $nsp += $_ if $align eq "j"; | ||
| 959 | } | ||
| 960 | } | ||
| 961 | # Collect final ts vector. | ||
| 962 | push (@$tt, $t) if @$t; | ||
| 963 | |||
| 964 | # Calculate initial position, and set it. | ||
| 965 | $xi = $width - $wd if $align eq "r"; | ||
| 966 | $xi = ($width - $wd)/2 if $align eq "c"; | ||
| 967 | $ret .= sprintf ("%.2f %.2f moveto\n", $x+$xi, $y); | ||
| 968 | $maxwidth = $wd if $wd > $maxwidth; | ||
| 969 | |||
| 970 | # Calculate amount of stretch needed. | ||
| 971 | my $stretch = 1; | ||
| 972 | $stretch = ($width - $wd + $nsp) / $nsp | ||
| 973 | if $align eq "j" && $nsp; | ||
| 974 | |||
| 975 | # Process the vectors. | ||
| 976 | foreach $t ( @$tt ) { | ||
| 977 | $cur = shift(@$t); | ||
| 978 | next unless @$t; | ||
| 979 | my $needsave = $cur->ps_setfont; | ||
| 980 | if ( $needsave ) { | ||
| 981 | $ret .= "gsave\n"; | ||
| 982 | $self->ps_setfont(undef); | ||
| 983 | } | ||
| 984 | $ret .= $needsave; | ||
| 985 | $t = [map { ref($_) ? @$_ : $_*$stretch } @$t]; | ||
| 986 | $ret .= $cur->ps_tj ($t); | ||
| 987 | $ret .= "currentpoint grestore moveto\n" if $needsave; | ||
| 988 | $did++; | ||
| 989 | } | ||
| 990 | $did; | ||
| 991 | }; | ||
| 992 | |||
| 993 | # Subroutine code starts here. | ||
| 994 | |||
| 995 | # Preprocessing of the arguments. Keep everything, but split the | ||
| 996 | # strings. | ||
| 997 | my @args; | ||
| 998 | my $preparse; | ||
| 999 | $preparse = sub { | ||
| 1000 | my $t = shift; | ||
| 1001 | foreach ( @$t ) { | ||
| 1002 | if ( ref($_) ) { | ||
| 1003 | if ( UNIVERSAL::isa($_,PostScript::BasicTypesetter::) ) { | ||
| 1004 | push (@args, $cur = $_); | ||
| 1005 | next; | ||
| 1006 | } | ||
| 1007 | # Array ref introduces new context. | ||
| 1008 | # Tack current font at end. | ||
| 1009 | push (@$_, $cur); | ||
| 1010 | $preparse->($_); | ||
| 1011 | next; | ||
| 1012 | } | ||
| 1013 | s/\s+/ /g if $align eq "j"; | ||
| 1014 | push (@args, split (/( )/, $_)); | ||
| 1015 | } | ||
| 1016 | }; | ||
| 1017 | $t = [$t] unless ref($t) && ref($t) eq "ARRAY"; | ||
| 1018 | $preparse->($t); | ||
| 1019 | |||
| 1020 | # @args is now a list of strings (some of which are a single space), | ||
| 1021 | # and typesetter objects. | ||
| 1022 | # print STDERR Data::Dumper->Dump([\@args],[qw{args}]); | ||
| 1023 | |||
| 1024 | $cur = $cur0; | ||
| 1025 | $switch_font->(); | ||
| 1026 | foreach my $arg ( @args ) { | ||
| 1027 | |||
| 1028 | # Skip empty args resulting from strings that start with a space. | ||
| 1029 | next if $arg eq ''; | ||
| 1030 | |||
| 1031 | # Typesetter object. | ||
| 1032 | if ( ref($arg) ) { | ||
| 1033 | push (@res, $cur = $arg); | ||
| 1034 | $switch_font->(); | ||
| 1035 | next; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | # Width of this thing. | ||
| 1039 | my $w = $arg eq " " ? $wspace : $cur->stringwidth($arg); | ||
| 1040 | |||
| 1041 | # See if it fits. | ||
| 1042 | if ( $wd + $w > $width ) { | ||
| 1043 | |||
| 1044 | # No. Fill what we have. | ||
| 1045 | if ( $flush->() ) { | ||
| 1046 | # Advance to next line. | ||
| 1047 | $ret .= $cur00->ps_linebreak(\$y); | ||
| 1048 | # Reset. | ||
| 1049 | @res = (); | ||
| 1050 | $wd = 0; | ||
| 1051 | $xi = 0; | ||
| 1052 | $overflow++; | ||
| 1053 | } | ||
| 1054 | $cur0 = $cur; | ||
| 1055 | |||
| 1056 | } | ||
| 1057 | # It fits -> append. | ||
| 1058 | # Push strings as a [width,kstring] pair, and spaces as a | ||
| 1059 | # number. | ||
| 1060 | push (@res, $arg eq " " ? $w : [$w, $cur->metrics->kstring($arg)]); | ||
| 1061 | $wd += $w; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | # Process remainder. | ||
| 1065 | if ( @res ) { | ||
| 1066 | $align = "l" if $align eq "j"; # disable justify | ||
| 1067 | $flush->(); | ||
| 1068 | # Update indent value. | ||
| 1069 | $xi = $wd; | ||
| 1070 | } | ||
| 1071 | elsif ( ref($yy) ) { | ||
| 1072 | $y += $lskip; # already updated, so fix it | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | # Update return values. | ||
| 1076 | $$yy = $y if ref($yy); | ||
| 1077 | $$xxi = $xi if ref($xxi); | ||
| 1078 | $cur00->{textwidth} = $maxwidth; | ||
| 1079 | $ret; | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | # Internal helper routine. This is for the single-font case. | ||
| 1083 | # About 30% faster. | ||
| 1084 | sub _ps_simpletextbox { | ||
| 1085 | my ($self, $x, $xxi, $yy, $width, $t, $align) = @_; | ||
| 1086 | |||
| 1087 | # Deref arguments, if needed. | ||
| 1088 | my $xi = ref($xxi) ? $$xxi : $xxi; | ||
| 1089 | my $y = ref($yy) ? $$yy : $yy; | ||
| 1090 | |||
| 1091 | # Accumulated output. | ||
| 1092 | my $ret = ''; | ||
| 1093 | my $needsave = $self->ps_setfont(); | ||
| 1094 | if ( $needsave ) { | ||
| 1095 | $ret .= "gsave\n"; | ||
| 1096 | $self->ps_setfont(undef); | ||
| 1097 | } | ||
| 1098 | $ret .= $needsave; | ||
| 1099 | |||
| 1100 | # Scaling for fill, only when justifying. | ||
| 1101 | my $scale = ($align eq 'j') ? FONTSCALE/$self->fontsize : 0; | ||
| 1102 | |||
| 1103 | # Width of a space. | ||
| 1104 | my $wspace = $self->_stringwidth(" "); | ||
| 1105 | croak ("ps_textbox: [".$self->real_fontname."]: missing space") | ||
| 1106 | unless $wspace > 0; | ||
| 1107 | |||
| 1108 | # Accumulated width. | ||
| 1109 | my $wd = $xi + -$wspace; | ||
| 1110 | |||
| 1111 | # Line skip (baselines). | ||
| 1112 | my $lineskip = $self->{lineskip}; | ||
| 1113 | |||
| 1114 | # Characters on which a word may split | ||
| 1115 | my $hyphens = $self->{hyphens}; | ||
| 1116 | |||
| 1117 | # Make pattern. | ||
| 1118 | $hyphens = "([$hyphens])([^$hyphens\\s]+[$hyphens]?)" if $hyphens; | ||
| 1119 | |||
| 1120 | my @res; | ||
| 1121 | my $maxwidth = 0; # max width of textbox | ||
| 1122 | |||
| 1123 | my $flush = sub { | ||
| 1124 | my $ext = 0; | ||
| 1125 | $ext = $scale*(($width - $wd) / (@res-1)) | ||
| 1126 | if $align eq "j" && $scale && @res > 1; | ||
| 1127 | my $t = $self->metrics->kstring ("@res", $ext); | ||
| 1128 | |||
| 1129 | # Calculate initial position, and set it. | ||
| 1130 | $xi = $width - $wd if $align eq "r"; | ||
| 1131 | $xi = ($width - $wd)/2 if $align eq "c"; | ||
| 1132 | $ret .= sprintf ("%.2f %.2f moveto\n", $x+$xi, $y); | ||
| 1133 | $ret .= $self->ps_tj ($t); | ||
| 1134 | $maxwidth = $wd if $wd > $maxwidth; | ||
| 1135 | }; | ||
| 1136 | |||
| 1137 | # Subroutine code starts here. | ||
| 1138 | |||
| 1139 | # Split into space-separated pieces (let's call them "words"). | ||
| 1140 | my @text = split (/\s/, $t, -1); | ||
| 1141 | foreach my $str ( @text ) { | ||
| 1142 | # Width of this "word". | ||
| 1143 | my $w = $self->stringwidth($str); | ||
| 1144 | my $remainder = ''; | ||
| 1145 | # If it doesn't fit, see if word can be split | ||
| 1146 | if ( $hyphens and $wd + $wspace + $w > $width ) { | ||
| 1147 | my $str2 = $str; | ||
| 1148 | my $w2 = $w; | ||
| 1149 | while ($wd + $wspace + $w2 > $width) { | ||
| 1150 | last unless $str2 =~ s|$hyphens$|$1|; | ||
| 1151 | $remainder = $2 . $remainder; | ||
| 1152 | $w2 = $self->stringwidth($str2); | ||
| 1153 | } | ||
| 1154 | if ( length $remainder ) { | ||
| 1155 | if ( $wd + $wspace + $w2 > $width ) { | ||
| 1156 | $remainder = ''; | ||
| 1157 | } | ||
| 1158 | else { | ||
| 1159 | push @res, $str2; | ||
| 1160 | $w = $w2; | ||
| 1161 | $str = $str2; | ||
| 1162 | } | ||
| 1163 | } | ||
| 1164 | } | ||
| 1165 | # See if it fits. | ||
| 1166 | if ( (@res && $wd + $wspace + $w > $width) or length($remainder) ) { | ||
| 1167 | # No -> flush what we have. | ||
| 1168 | $flush->(); | ||
| 1169 | # Advance to next line. | ||
| 1170 | $ret .= $self->ps_linebreak(\$y); | ||
| 1171 | # Reset. | ||
| 1172 | @res = (); | ||
| 1173 | $xi = 0; | ||
| 1174 | $wd = -$wspace; | ||
| 1175 | if ( length($remainder) ) { | ||
| 1176 | $str = $remainder; | ||
| 1177 | $w = $self->stringwidth($remainder); | ||
| 1178 | } | ||
| 1179 | } | ||
| 1180 | # It fits -> append. | ||
| 1181 | $wd += $wspace + $w; | ||
| 1182 | push (@res, $str); | ||
| 1183 | } | ||
| 1184 | # Process remainder. | ||
| 1185 | if ( @res ) { | ||
| 1186 | $align = "l"if $align eq "j"; | ||
| 1187 | $flush->(); | ||
| 1188 | # Update indent value. | ||
| 1189 | $xi = $wd; | ||
| 1190 | } | ||
| 1191 | elsif ( ref($yy) ) { | ||
| 1192 | $y += $lineskip; # already updated, so fix it | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | # Update return values. | ||
| 1196 | $$yy = $y if ref($yy); | ||
| 1197 | $$xxi = $xi if ref($xxi); | ||
| 1198 | $self->{textwidth} = $maxwidth; | ||
| 1199 | $ret .= "currentpoint grestore moveto\n" if $needsave; | ||
| 1200 | $ret; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | 1; | ||
| 1204 | |||
| 1205 | __END__ |