| 1 | package PDF::API2::Resource::UniFont; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '2.044'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Encode qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my $class = shift(); | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=21 SCAR=43.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | $class = ref($class) if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 1 | my $self = { | |||||||||||||||||||||||||||
| 15 | fonts => [], | ||||||||||||||||||||||||||||
| 16 | block => {}, | ||||||||||||||||||||||||||||
| 17 | code => {}, | ||||||||||||||||||||||||||||
| 18 | pdf => shift(), | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | my @fonts; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | push @fonts, shift() while ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my %options = @_; | |||||||||||||||||||||||||||
| 26 | 1 | ▶ | $self->{'encode'} = $options{'-encode'} if defined $options{'-encode'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | my $font_number = 0; | |||||||||||||||||||||||||||
| 29 | 1 | foreach my $font (@fonts) { | |||||||||||||||||||||||||||
| 30 | 2 | ▶ | if (ref($font) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 31 | 1 | push @{$self->{'fonts'}}, shift(@$font); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | while (defined $font->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | my $blockspec = shift @$font; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | if (ref($blockspec)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | foreach my $block ($blockspec->[0] .. $blockspec->[-1]) { | |||||||||||||||||||||||||||
| 37 | 1 | $self->{'block'}->{$block} = $font_number; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||||||||||
| 41 | 0 | $self->{'block'}->{$blockspec} = $font_number; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | elsif (ref($font) eq 'HASH') { | ||||||||||||||||||||||||||||
| 46 | 0 | push @{$self->{'fonts'}}, $font->{'font'}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (defined($font->{'blocks'}) and ref($font->{'blocks'}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $font->{"blocks"} and ref $font->{"blocks"} eq "ARRAY"
MC/DC0%0/2
defined $font->{"blocks"} and ref $font->{"blocks"} eq "ARRAY"
defined $font->{"blocks"}
ref $font->{"blocks"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $font->{"blocks"}B= ref $font->{"blocks"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 49 | 0 | foreach my $blockspec (@{$font->{'blocks'}}) { | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (ref($blockspec)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | foreach my $block ($blockspec->[0] .. $blockspec->[-1]) { | |||||||||||||||||||||||||||
| 52 | 0 | $self->{'block'}->{$block} = $font_number; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | 0 | $self->{'block'}->{$blockspec} = $font_number; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (defined($font->{'codes'}) and ref($font->{'codes'}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $font->{"codes"} and ref $font->{"codes"} eq "ARRAY"
MC/DC0%0/2
defined $font->{"codes"} and ref $font->{"codes"} eq "ARRAY"
defined $font->{"codes"}
ref $font->{"codes"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $font->{"codes"}B= ref $font->{"codes"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 62 | 0 | foreach my $codespec (@{$font->{'codes'}}) { | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if (ref($codespec)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | foreach my $code ($codespec->[0] .. $codespec->[-1]) { | |||||||||||||||||||||||||||
| 65 | 0 | $self->{'code'}->{$code} = $font_number; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 0 | $self->{'code'}->{$codespec} = $font_number; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||
| 75 | 1 | push @{$self->{'fonts'}}, $font; | |||||||||||||||||||||||||||
| 76 | 1 | foreach my $block (0 .. 255) { | |||||||||||||||||||||||||||
| 77 | 256 | $self->{'block'}->{$block} = $font_number; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 2 | $font_number++; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub isvirtual { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
isvirtual: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub fontlist { | ||||||||||||||||||||||||||||
| 89 | 11 | ▶ | my $self = shift(); | ||||||||||||||||||||||||||
|
Subroutine
fontlist: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 11 | return [@{$self->{'fonts'}}]; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub width { | ||||||||||||||||||||||||||||
| 94 | 4 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
width: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 4 | my $width = 0; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | my @blocks = (); | |||||||||||||||||||||||||||
| 99 | 4 | foreach my $u (unpack('U*', $text)) { | |||||||||||||||||||||||||||
| 100 | 18 | my $font_number = 0; | |||||||||||||||||||||||||||
| 101 | 18 | ▶ | if (defined $self->{'code'}->{$u}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 102 | 0 | $font_number = $self->{'code'}->{$u}; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | elsif (defined $self->{'block'}->{$u >> 8}) { | ||||||||||||||||||||||||||||
| 105 | 18 | $font_number = $self->{'block'}->{$u >> 8}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||
| 108 | 0 | $font_number = 0; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 18 | ▶ | if (scalar @blocks == 0 or $blocks[-1]->[0] != $font_number) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @blocks == 0 or $blocks[-1][0] != $font_number
MC/DC100%2/2
scalar @blocks == 0 or $blocks[-1][0] != $font_number
scalar @blocks == 0
$blocks[-1][0] != $font_number
Truth table100%3/3
A or B
A= scalar @blocks == 0B= $blocks[-1][0] != $font_number
| |||||||||||||||||||||||||||||
| 112 | 6 | push @blocks, [$font_number, pack('U', $u)]; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 12 | $blocks[-1]->[1] .= pack('U', $u); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | foreach my $block (@blocks) { | |||||||||||||||||||||||||||
| 120 | 6 | my ($font_number, $string) = @$block; | |||||||||||||||||||||||||||
| 121 | 6 | $width += $self->fontlist->[$font_number]->width($string); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4 | return $width; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub text { | ||||||||||||||||||||||||||||
| 128 | 2 | ▶ | my ($self, $text, $size, $indent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | $text = decode($self->{'encode'}, $text) unless utf8::is_utf8($text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | croak 'Font size not specified' unless defined $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | my $value = ''; | |||||||||||||||||||||||||||
| 133 | 2 | my $last_font_number; | |||||||||||||||||||||||||||
| 134 | my @codes; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | foreach my $u (unpack('U*', $text)) { | |||||||||||||||||||||||||||
| 137 | 9 | my $font_number = 0; | |||||||||||||||||||||||||||
| 138 | 9 | ▶ | if (defined $self->{'code'}->{$u}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 139 | 0 | $font_number = $self->{'code'}->{$u}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | elsif (defined $self->{'block'}->{$u >> 8}) { | ||||||||||||||||||||||||||||
| 142 | 9 | $font_number = $self->{'block'}->{$u >> 8}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 9 | ▶ | if (defined $last_font_number and $font_number != $last_font_number) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $last_font_number and $font_number != $last_font_number
MC/DC100%2/2
defined $last_font_number and $font_number != $last_font_number
defined $last_font_number
$font_number != $last_font_number
Truth table100%3/3
A and B
A= defined $last_font_numberB= $font_number != $last_font_number
| |||||||||||||||||||||||||||||
| 146 | 1 | my $font = $self->fontlist->[$last_font_number]; | |||||||||||||||||||||||||||
| 147 | 1 | $value .= '/' . $font->name() . ' ' . $size . ' Tf '; | |||||||||||||||||||||||||||
| 148 | 1 | $value .= $font->text(pack('U*', @codes), $size, $indent) . ' '; | |||||||||||||||||||||||||||
| 149 | 1 | $indent = undef; | |||||||||||||||||||||||||||
| 150 | 1 | @codes = (); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 9 | push @codes, $u; | |||||||||||||||||||||||||||
| 154 | 9 | $last_font_number = $font_number; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | if (scalar @codes > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 2 | my $font = $self->fontlist->[$last_font_number]; | |||||||||||||||||||||||||||
| 159 | 2 | $value .= '/' . $font->name() . ' ' . $size . ' Tf '; | |||||||||||||||||||||||||||
| 160 | 2 | $value .= $font->text(pack('U*', @codes), $size, $indent); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | return $value; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | 1; | |||||||||||||||||||||||||||