View | Details | Raw Unified | Return to bug 2499
Collapse All | Expand All

(-)a/C4/Labels/Label.pm (-7 / +75 lines)
Lines 3-9 package C4::Labels::Label; Link Here
3
use strict;
3
use strict;
4
use warnings;
4
use warnings;
5
5
6
use Text::Wrap;
7
use Algorithm::CheckDigits;
6
use Algorithm::CheckDigits;
8
use Text::CSV_XS;
7
use Text::CSV_XS;
9
use Data::Dumper;
8
use Data::Dumper;
Lines 381-386 sub create_label { Link Here
381
    return;
380
    return;
382
}
381
}
383
382
383
sub wrap_fields_proportional {
384
    my ($self, %params) = @_;
385
    my $font = $params{'font'};
386
    my $font_size = $params{'font_size'};
387
    my $field_data = $params{'field_data'};
388
    my @lines;
389
390
    my $field_length = length $field_data;
391
    my @fieldchar_map;
392
    my @fieldwidth_map;
393
    my @fieldcharwidthsum_map;
394
    my $fieldcharwidthsum = 0.0;  # adjusted for margins to align with draw_label_text
395
    my $line_startpos = 0;
396
    my $ignore_subsequent_lines = 0;
397
398
    # build lookup maps and use them to scan backward to break on space when truncation would be detected.
399
    my $idx = 0;
400
    for ( $idx=0;  $idx <= $field_length; $idx++ ) {
401
        my $fieldchar =  substr $field_data, $idx, 1;
402
        $fieldchar_map[$idx] = $fieldchar;
403
404
        my $fieldchar_width = C4::Creators::PDF->StrWidth($fieldchar, $font, $self->{'font_size'});
405
        $fieldcharwidthsum =  $fieldcharwidthsum + $fieldchar_width;
406
        $fieldcharwidthsum_map[$idx] = $fieldcharwidthsum; # accumulation of field widths for this field_data
407
408
        if ( $fieldcharwidthsum > ($self->{'width'} - $self->{'left_text_margin'}) ) {  # truncation detect
409
410
            # search backward until space to break text on.
411
            my $space_start_idx = $idx - 1;   # start from current position - 1
412
413
            SPACESCAN: for ( my $space_idx = $space_start_idx; $space_idx >= 0; $space_idx--) { # in reverse
414
415
                if ( $fieldchar_map[$space_idx] eq ' ' ) {
416
                    my $line_length = $space_idx - $line_startpos;
417
                    my $text_line = substr $field_data, $line_startpos, $line_length;
418
                    push @lines, $text_line;
419
420
                    if ( ! $self->{'text_wrap_cols'} ) { $ignore_subsequent_lines = 1; }  # so we don't write more
421
422
                    $line_startpos = $space_idx + 1;
423
424
                    # re-adjust fieldcharwidthsum to start from line_startpos thru current idx of master loop
425
                    $fieldcharwidthsum = 0.0;
426
                    for ( my $widthsum_idx = $line_startpos; $widthsum_idx <= $idx;  $widthsum_idx++ ) {
427
                        my $fc = substr $field_data, $widthsum_idx, 1;
428
                        my $fcw = C4::Creators::PDF->StrWidth($fc, $font, $self->{'font_size'});
429
                        $fieldcharwidthsum = $fieldcharwidthsum + $fcw;
430
                    } 
431
432
                    last SPACESCAN;
433
                }
434
            }
435
        }
436
    }
437
438
    # flush what didn't get flushed from truncation if we haven't written anything yet.
439
    if ( $ignore_subsequent_lines == 0 ) {
440
       my $line_length = $field_length - $line_startpos;
441
       my $text_line = substr $field_data, $line_startpos, $line_length;
442
       push @lines, $text_line;
443
    }
444
445
    return \@lines;
446
}
447
384
sub draw_label_text {
448
sub draw_label_text {
385
    my ($self, %params) = @_;
449
    my ($self, %params) = @_;
386
    my @label_text = ();
450
    my @label_text = ();
Lines 430-437 sub draw_label_text { Link Here
430
                $field_data =~ s/\(/\\\(/g;    # Escape '(' and ')' for the pdf object stream...
494
                $field_data =~ s/\(/\\\(/g;    # Escape '(' and ')' for the pdf object stream...
431
                $field_data =~ s/\)/\\\)/g;
495
                $field_data =~ s/\)/\\\)/g;
432
            }
496
            }
433
            eval{$Text::Wrap::columns = $self->{'text_wrap_cols'};};
497
434
            my @line = split(/\n/ ,wrap('', '', $field_data));
498
            my @line = @{ wrap_fields_proportional( $self,
499
                font => $font,
500
                font_size => $self->{'font_size'},
501
                field_data => $field_data
502
                ) }; 
503
435
            # If this is a title field, limit to two lines; all others limit to one... FIXME: this is rather arbitrary
504
            # If this is a title field, limit to two lines; all others limit to one... FIXME: this is rather arbitrary
436
            if ($field->{'code'} eq 'title' && scalar(@line) >= 2) {
505
            if ($field->{'code'} eq 'title' && scalar(@line) >= 2) {
437
                while (scalar(@line) > 2) {
506
                while (scalar(@line) > 2) {
Lines 452-460 sub draw_label_text { Link Here
452
                $text_llx = $params{'llx'} + $self->{'width'} - ($self->{'left_text_margin'} + $string_width);
521
                $text_llx = $params{'llx'} + $self->{'width'} - ($self->{'left_text_margin'} + $string_width);
453
            }
522
            }
454
            elsif($self->{'justify'} eq 'C') {
523
            elsif($self->{'justify'} eq 'C') {
455
                 # some code to try and center each line on the label based on font size and string point width...
524
                # some code to try and center each line on the label based on font size and string point width...
456
                 my $whitespace = ($self->{'width'} - ($string_width + (2 * $self->{'left_text_margin'})));
525
                my $whitespace = ($self->{'width'} - ($string_width + (2 * $self->{'left_text_margin'})));
457
                 $text_llx = (($whitespace  / 2) + $params{'llx'} + $self->{'left_text_margin'});
526
                $text_llx = (($whitespace  / 2) + $params{'llx'} + $self->{'left_text_margin'});
458
            }
527
            }
459
            else {
528
            else {
460
                $text_llx = ($params{'llx'} + $self->{'left_text_margin'});
529
                $text_llx = ($params{'llx'} + $self->{'left_text_margin'});
461
- 

Return to bug 2499