Layout/MarkupText.pm untested

filter: Statementstmt 0.0% 0 / 215 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 359 CC 33 SCAR 38.0
CC
33
Cov
0%
_tm_startelement
182.0
_tm_lookup_font
20.0
_markup_to_objects
20.0
CRAP 44.5
1 #=======================================================================
2 # ____ ____ _____ _ _
3 # | _ \| _ \| ___| _ _ | | __ _ _ _ ___ _ _| |_
4 # | |_) | | | | |_ (_) (_) | | / _` | | | |/ _ \| | | | __|
5 # | __/| |_| | _| _ _ | |__| (_| | |_| | (_) | |_| | |_
6 # |_| |____/|_| (_) (_) |_____\__,_|\__, |\___/ \__,_|\__|
7 # |___/
8 #
9 # A Perl Module Chain to faciliate Layouts for PDF::API2.
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 FILE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
21 # AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 # SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS
25 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS FILE, EVEN IF
32 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34 # SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
35 #
36 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
37 # LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE
38 # FREE SOFTWARE FOUNDATION, INC., 59 TEMPLE PLACE - SUITE 330,
39 # BOSTON, MA 02111-1307, USA.
40 #
41 # $Id: $
42 #
43 #=======================================================================
44  
45 package PDF::APIx::Layout::MarkupText;
46  
47 BEGIN
48 {
49 use utf8;
50 use Encode qw(:all);
51 use POSIX qw[ floor ceil ];
52 use XML::Parser;
53  
54 use PDF::API2::UniWrap;
55 use PDF::APIx::Layout::RawTextObjects;
56  
57 use vars qw( @ISA $VERSION );
58  
59 @ISA=qw[ PDF::APIx::Layout::RawTextObjects ];
60  
61 ($VERSION) = '$Revision: $' =~ /Revision: (\S+)\s/; # $Date: $
62 }
63  
64 no warnings qw[ deprecated recursion uninitialized ];
65  
66 sub _tm_calc_style_fallback
67 {
68 my @style_prelookup=@_;
69 my %style_fallback=(
70 'oblique' => [qw{ italic normal regular }],
71 'italic' => [qw{ oblique normal regular }],
72 'normal' => [qw{ regular }],
73 'regular' => [qw{ normal }],
74 );
75  
76 my @style_lookup=();
77 my %reg=();
78 my $w;
79 while($w = lc shift @style_prelookup)
80 {
81 unless(defined $reg{$w})
82 {
83 push @style_lookup, $w;
84 push @style_prelookup, @{$style_fallback{$w}} if(defined $style_fallback{$w});
85 $reg{$w}=1;
86 }
87 }
88
89 return(@style_lookup);
90 }
91 sub _tm_calc_weight_fallback
92 {
93 my @weight_prelookup=@_;
94 my %weight_fallback=(
95 'thin' => [qw{ ultralight extralight light normal regular }],
96 'ultralight' => [qw{ extralight light normal regular }],
97 'extralight' => [qw{ light normal regular }],
98 'light' => [qw{ normal regular }],
99 'normal' => [qw{ regular }],
100 'medium' => [qw{ demibold semibold bold }],
101 'demibold' => [qw{ semibold bold }],
102 'semibold' => [qw{ bold }],
103 'heavy' => [qw{ extrabold ultrabold bold }],
104 'extrabold' => [qw{ ultrabold bold }],
105 'ultrabold' => [qw{ bold }],
106 );
107  
108 my @weight_lookup=();
109 my %reg=();
110 my $w;
111 while($w = lc shift @weight_prelookup)
112 {
113 unless(defined $reg{$w})
114 {
115 push @weight_lookup, $w;
116 push @weight_prelookup, @{$weight_fallback{$w}} if(defined $weight_fallback{$w});
117 $reg{$w}=1;
118 }
119 }
120
121 return(@weight_lookup);
122 }
123 sub _tm_calc_stretch
124 {
125 my $s = shift @_;
126 my %stretch=qw[
127 ultracondensed 0.500
128 extracondensed 0.625
129 condensed 0.750
130 semicondensed 0.875
131 normal 1
132 medium 1
133 semiexpanded 1.125
134 expanded 1.250
135 extraexpanded 1.500
136 ultraexpanded 2
137 ];
138 return($s) if($s=~m|^[\d\.]+$|);
139 return($stretch{$s}) if(defined $stretch{$s});
140 return(1);
141 }
142 sub _tm_calc_size
143 {
144 my $s = shift @_;
145 my %size=qw[
146 xx-small 6
147 x-small 8
148 small 10
149 medium 12
150 normal 12
151 large 14
152 x-large 18
153 xx-large 24
154 ];
155 return($s) if($s=~m|^[\d\.]+$|);
156 return($size{$s}) if(defined $size{$s});
157 return(10);
158 }
159 sub _tm_calc_rise
160 {
161 my ($s,$sz)=@_;
162 my %rise=qw[
163 superscript 0.5
164 subscript -0.35
165 none 0
166 ];
167 return($s) if($s=~m|^[\-\d\.]+$|);
168 return($rise{$s}*$sz) if(defined $rise{$s});
169 return(0);
170 }
171  
172 #----------------------------------
173 # register font for face/weight/style
174 sub _tm_register_face
175 {
176 my ($self,$font,$face,$weight,$style)=@_;
177 $face=lc($face);
178 $face=~s|[^a-z0-9]+||go;
179 $weight=lc($weight);
180 $weight=~s|[^a-z0-9]+||go;
181 $style=lc($style);
182 $style=~s|[^a-z0-9]+||go;
183 if($face eq 'default')
184 {
185 $self->{-fonts}->{$face}=$font;
186 }
187 else
188 {
189 $self->{-fonts}->{"$face:$weight:$style"}=$font;
190 }
191 }
192  
193 #----------------------------------
194 # looking up the spec. face/weight/style or
195 # falling back to face-only
196 sub _tm_lookup_face
197 {
198 my ($self,$face,$weight,$style)=@_;
199 $face=lc($face);
200 $face=~s|[^a-z0-9]+||go;
201 $weight=lc($weight);
202 $weight=~s|[^a-z0-9]+||go;
203 $style=lc($style);
204 $style=~s|[^a-z0-9]+||go;
205 return $self->{-fonts}->{"$face:$weight:$style"}
206 if(defined $self->{-fonts}->{"$face:$weight:$style"});
207 return $self->{-fonts}->{$face}
208 if(defined $self->{-fonts}->{$face});
209 # return $self->{-fonts}->{default}
210 # if(defined $self->{-fonts}->{default});
211 return(undef);
212 }
213  
214 #----------------------------------
215 # lookup the spec. font or falling back to the default
216 sub _tm_lookup_font
217 {
218 my ($self,$face,$weight,$style)=@_;
219 my $font=undef;
220 foreach my $weightlookup (_tm_calc_weight_fallback(lc $weight))
221 {
222 foreach my $stylelookup (_tm_calc_style_fallback(lc $style))
223 {
224 $font=$self->_tm_lookup_face($face,$weightlookup,$stylelookup);
225 last if(defined $font);
226 }
227 last if(defined $font);
228 }
229 $font=$self->_tm_lookup_face('default') unless(defined $font);
230 return($font);
231 }
232 sub _tm_push_span
233 {
234 my ($xp,$span)=@_;
235 push @{$xp->{-pdf}->{content}},$span;
236 }
237 sub _tm_push_content
238 {
239 my ($xp,$str)=@_;
240 $xp->{-pdf}->{content}->[-1]->[-1].=$str;
241 }
242 sub _tm_push
243 {
244 my ($xp,%state)=@_;
245 push @{$xp->{-pdf}->{stack}},{%state};
246 }
247 sub _tm_pop
248 {
249 my ($xp)=@_;
250 return(%{pop @{$xp->{-pdf}->{stack}}});
251 }
252 sub _tm_peek
253 {
254 my ($xp)=@_;
255 return(%{$xp->{-pdf}->{stack}->[-1]});
256 }
257 sub _tm_init
258 {
259 my ($xp)=@_;
260 #print STDERR "INIT.\n";
261 }
262 sub _tm_final
263 {
264 my ($xp)=@_;
265 #print STDERR "FINAL.\n";
266 }
267 sub _tm_startelement
268 {
269 my ($xp,$element,%attr)=@_;
270 #print STDERR "START: $element\n";
271 my %state=_tm_peek($xp);
272 if($element eq 'span' || $element eq 'p')
273 {
274 %state=(%state,%attr);
275 }
276 elsif($element eq 'b')
277 {
278 $state{weight}='bold';
279 }
280 elsif($element eq 'i')
281 {
282 $state{style}='italic';
283 }
284 elsif($element eq 'sub')
285 {
286 $state{rise}=(-$attr{value})||'subscript';
287 $state{risesize}=$attr{size}||'normal';
288 }
289 elsif($element eq 'sup')
290 {
291 $state{rise}=$attr{value}||'superscript';
292 $state{risesize}=$attr{size}||'normal';
293 }
294 elsif($element eq 'u')
295 {
296 $state{underline}=$attr{type}||'single';
297 }
298 elsif($element eq 'c')
299 {
300 $state{stretch}=$attr{value}||'condensed';
301 }
302 elsif($element eq 'e')
303 {
304 $state{stretch}=$attr{value}||'expanded';
305 }
306 elsif($element eq 'a')
307 {
308 if(defined $attr{id})
309 {
310 $state{id}=$attr{id};
311 }
312 if(defined $attr{name})
313 {
314 $state{id}=$attr{name};
315 }
316 if(defined $attr{href})
317 {
318 $state{href}=$attr{href};
319 }
320 }
321 _tm_push($xp,%state);
322 _tm_push_span($xp,[{%state},'']);
323 }
324 sub _tm_endelement
325 {
326 my ($xp,$element)=@_;
327 #print STDERR "END: $element\n";
328 _tm_pop($xp);
329 my %state=_tm_peek($xp);
330 _tm_push_span($xp,[{%state},'']);
331 }
332 sub _tm_char
333 {
334 my ($xp,$str)=@_;
335 _tm_push_content($xp,$str);
336 #print STDERR 'DEBUG: '.Dumper($xp);
337 #print STDERR "CHAR: '$str'\n";
338 }
339 sub _tm_debug
340 {
341 #print STDERR 'DEBUG: '.Dumper(\@_);
342 return('unknown');
343 }
344 sub _tm_null
345 {
346 #print STDERR "NULL.\n";
347 return('unknown');
348 }
349 # <p>
350 # x= <n>
351 # y= <n>
352 # width= <n>
353 # height= <n>
354 # bgcolor= #00FF00
355 # | <name>
356 #
357 # <i> <b> <u> <s> <c> <e> <sub> <sup> <br/>
358 #
359 # <span>
360 # face= serif
361 # | sans
362 # | mono
363 # | <s>
364 # | Courier
365 # | Georgia
366 # | Helvetica
367 # | Times
368 # | Trebuchet
369 # | Verdana
370 #
371 # size= xx-small (6pt)
372 # | x-small (8pt)
373 # | small (10pt)
374 # | medium (12pt)
375 # | normal (12pt)
376 # | large (14pt)
377 # | x-large (18pt)
378 # | xx-large (24pt)
379 # | <n>
380 #
381 # style= normal
382 # | regular
383 # | italic
384 # | oblique
385 #
386 # weight= thin (100)
387 # | ultralight (200)
388 # | extralight (200)
389 # | light (300)
390 # | normal (400)
391 # | regular (400)
392 # | medium (500)
393 # | demibold (600)
394 # | semibold (600)
395 # | bold (700)
396 # | ultrabold (800)
397 # | extrabold (800)
398 # | heavy (900)
399 # | <n>
400 #
401 #
402 # stretch= ultracondensed (0.500)
403 # | extracondensed (0.625)
404 # | condensed (0.750)
405 # | semicondensed (0.875)
406 # | normal (1.000)
407 # | medium (1.000)
408 # | semiexpanded (1.125)
409 # | expanded (1.250)
410 # | extraexpanded (1.500)
411 # | ultraexpanded (2.000)
412 # | <n>
413 #
414 # color= #00FF00
415 # | <name>
416 #
417 # underline= single
418 # | double
419 # | low
420 # | none
421 #
422 # rise= superscript
423 # | subscript
424 # | none
425 # | <n>
426 #
427 #
428 # <a>
429 # id="somename" name="somename" %% create a "#somename" in the name-tree
430 #
431 # href="url" %% if url == m|^#| dest to name-tree
432 #
433 sub _markup_to_objects
434 {
435 my ($self,$xml,%opts)=@_;
436
437 if(defined $opts{-fontreg})
438 {
439 foreach my $reg (@{$opts{-fontreg}})
440 {
441 $self->_tm_register_face(@{$reg});
442 }
443 }
444
445 my $xp=new XML::Parser::Expat(ProtocolEncoding=>'UTF-8',NoExpand=>0,NoLWP=>1);
446 $xp->setHandlers(
447 #'Init' => \&_tm_init,
448 #'Final' => \&_tm_final,
449 'Start' => \&_tm_startelement,
450 'End' => \&_tm_endelement,
451 'Char' => \&_tm_char,
452 'Proc' => \&_tm_null,
453 'Comment' => \&_tm_null,
454 'CdataStart' => \&_tm_null,
455 'CdataEnd' => \&_tm_null,
456 'Default' => \&_tm_null,
457 'Unparsed' => \&_tm_null,
458 'Notation' => \&_tm_null,
459 'ExternEnt' => \&_tm_null,
460 'ExternEntFin' => \&_tm_null,
461 'Entity' => \&_tm_null,
462 'Doctype' => \&_tm_null,
463 'DoctypeFin' => \&_tm_null,
464 'XMLDecl' => \&_tm_null,
465 );
466 $xml=PDF::API2::Util->xmlMarkupDecl."\n".$xml;
467 $xp->{-pdf}={
468 'fonts'=>{},
469 'stack'=>[
470 {
471 'face'=>'times',
472 'size'=>'normal',
473 'style'=>'normal',
474 'weight'=>'normal',
475 'stretch'=>'normal',
476 'color'=>'black',
477 'underline'=>'none',
478 'rise'=>'none',
479 }
480 ],
481 'content'=>[],
482 };
483 $xp->parse($xml);
484 my $fo=[];
485 while(scalar @{$xp->{-pdf}->{content}} > 0)
486 {
487 my $chunk=shift @{$xp->{-pdf}->{content}};
488 next if($chunk->[1] eq '');
489  
490 $chunk->[0]->{-font}=$self->_tm_lookup_font($chunk->[0]->{face},$chunk->[0]->{weight},$chunk->[0]->{style});
491 $chunk->[0]->{-fontsize}=_tm_calc_size($chunk->[0]->{size});
492 $chunk->[0]->{-hspace}=_tm_calc_stretch($chunk->[0]->{stretch})*100;
493 $chunk->[0]->{-rise}=_tm_calc_rise($chunk->[0]->{rise},$chunk->[0]->{-fontsize});
494 $chunk->[0]->{-textcolor}=[$chunk->[0]->{color}];
495 $chunk->[0]->{-fontsize}=_tm_calc_size($chunk->[0]->{risesize}) if(defined $chunk->[0]->{risesize});
496  
497 delete $chunk->[0]->{face};
498 delete $chunk->[0]->{weight};
499 delete $chunk->[0]->{style};
500 delete $chunk->[0]->{size};
501 delete $chunk->[0]->{stretch};
502 delete $chunk->[0]->{color};
503  
504 $chunk->[0]->{-id}=$chunk->[0]->{id};
505 $chunk->[0]->{-href}=$chunk->[0]->{href};
506 $chunk->[0]->{-underline}=$chunk->[0]->{underline};
507  
508 delete $chunk->[0]->{id};
509 delete $chunk->[0]->{href};
510 delete $chunk->[0]->{underline};
511  
512 my $wrap=PDF::API2::UniWrap->new(line_length=>1,emergency_break=>100);
513 foreach my $t ( $wrap->break_lines($chunk->[1]) )
514 {
515 utf8::upgrade($t);
516 push @{$fo},[$chunk->[0],{},$t];
517 }
518 }
519
520 return($fo);
521 }
522  
523 sub new
524 {
525 my ($class,$xml,%opts)=@_;
526
527 $class=ref($class) ? ref($class) : $class;
528
529 my $self=$class->SUPER::_new(%opts);
530 my $fo=$self->_markup_to_objects($xml,%opts);
531 $self->_proc_objects($fo);
532  
533 return($self);
534 }
535  
536 1;
537  
538 __END__