File: 040_annotation

package info (click to toggle)
libpdf-builder-perl 3.027-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,992 kB
  • sloc: perl: 107,532; makefile: 10
file content (152 lines) | stat: -rw-r--r-- 6,044 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

# demonstrate (initially) open and closed annotations. the icons can be dragged
# elsewhere, and one annotation is initally open for update. if you make any
# posts (replies), you will be prompted to save the document before leaving.

use strict;
use warnings;

use lib '../lib';
use PDF::Builder;
use PDF::Builder::Util;

my $compress = 'none'; # uncompressed streams
#my $compress = 'flate'; # compressed streams

my ($ant, $ant2, $ant3, $ant4, $ant5, $ant6);
my $pdf = PDF::Builder->new(-compress => $compress);

#my $f1 = $pdf->corefont('Helvetica', -encode=>'latin1'); # unused
my $f2 = $pdf->corefont('Helvetica-Bold', -encode=>'latin1'); # page heading

my $page = $pdf->page();
$page->mediabox(595,842); # A4 paper

# just some random text near the top of the page
my $gfx = $page->gfx();
my $text = $page->text();
$text->textlabel(50,750, $f2,20, 'Hello World!', -color=>'red');

# draw a grid with 50pt blocks to see where rectangles are
$gfx->strokecolor("#CCC");
my $gridW = 500;
my $gridH = 700;
# offset everything by 10,10 to clear edges

# horizontal lines and labels, i is Y (0 at bottom)
for (my $i=0; $i<=$gridH; $i+=50) {
  $gfx->poly(10,$i+10, $gridW+10,$i+10);
  $text->textlabel($gridW+20,$i+8, $f2,10, $i);
}

# vertical lines and labels, i is X (0 at left)
for (my $i=0; $i<=$gridW; $i+=50) {
  $gfx->poly($i+10,10, $i+10,$gridH+10);
  $text->textlabel($i+8,$gridH+20, $f2,10, $i);
}
$gfx->stroke();

# initially open note (annotation), can be replied to multiple times by users.
# active area is supposed to be 100x100 at 50,150 (LL), but it seems to be a
# little larger than the visible icon! BTW, the icon can be dragged and dropped.
$ant = $page->annotation();
$ant->text("This is an initially open note.\nnext line", 
	   -color=>[ 0.8 ],  # light gray icon fill
	   -icon=>'Key',
	   -rect=>[60,10, 160,110], -open=>1);

# initially closed note (annotation), can be replied to multiple times by users.
# active area is supposed to be 100x100 at 200,300 (LL), but it seems to be 
# little larger than the visible icon! BTW, the icon can be dragged and dropped.
# note that a new annotation object must be created.
$ant2 = $page->annotation();
$ant2->text('This is an initially closed note', 
	   -color=>[ 0.3 ],  # dark gray icon fill
	   -text=>'Closed for the day!',  # extra note on rollover
	   -opacity=> 0.75, # a little translucency
	   # -border=>[10,10, 10],  # prominent border  N/A
	   # -icon   use default (Note)
	   -rect=>[210,110, 310,210]);

# ------------------
# some text on a new page to illustrate some more annotations
$page = $pdf->page();
$page->mediabox(595,842); # A4 paper

# just some random text near the top of the page
my $LoremIpsum = 
"Sed ut perspiciatis, unde omnis iste natus error sit ".
"voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, ".
"quae ab illo inventore veritatis et quasi architecto beatae vitae dicta ".
"sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur ".
"aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione ".
"dolor sit, voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ".
"ipsum, quia amet, consectetur, adipisci velit, sed quia non numquam eius ".
"modi tempora incidunt, ut labore et dolore magnam aliquam quaerat ".
"voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam ".
"corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?";

#$gfx = $page->gfx();
my $fontsize = 20;
my $descender = 3; # eyeball estimate
my $leading = $fontsize * 1.30;

$text = $page->text();
$text->font($f2, $fontsize);
$text->leading($leading);
$text->translate(75, 700);  # upper left baseline of 250-wide paragraph
my ($spill, $unused) = 
    $text->paragraph($LoremIpsum, 250,640, 0, -spillover=>1); 

# Now to do some annotations
$ant3 = $page->annotation();  # 1 line down from upper line
my $note = "This has a highlighter effect.";
my @topbot = y_topbot(700, 1, $leading, $fontsize, $descender);
my $corners = [115,$topbot[0], 221,$topbot[0], 115,$topbot[1], 221,$topbot[1]];
$ant3->markup($note, $corners, "Highlight", -color=>[1, .82, 0]);

$ant4 = $page->annotation();  # 7 lines down from upper line
$note = "This uses a squiggly line.";
@topbot = y_topbot(700, 7, $leading, $fontsize, $descender);
$corners = [75,$topbot[0], 298,$topbot[0], 75,$topbot[1], 298,$topbot[1]];
$ant4->markup($note, $corners, "Squiggly", -color=>[0, 0, 1]);

$ant5 = $page->annotation();  # 11 lines down from upper line
$note = "This uses an underline spanning two lines.";
@topbot = y_topbot(700, 11, $leading, $fontsize, $descender);
$corners = [277,$topbot[0], 330,$topbot[0], 277,$topbot[1], 330,$topbot[1],
            75,$topbot[0]-$leading, 157,$topbot[0]-$leading,
            75,$topbot[1]-$leading, 157,$topbot[1]-$leading];
$ant5->markup($note, $corners, "Underline", -color=>[0, 1, 0], 
             -text=>"Some title text");

$ant6 = $page->annotation();  # 15 lines down from upper line
$note = "This uses a strikeout spanning three lines.";
@topbot = y_topbot(700, 15, $leading, $fontsize, $descender);
$corners = [257,$topbot[0], 350,$topbot[0], 257,$topbot[1], 350,$topbot[1],
            75,$topbot[0]-$leading, 340,$topbot[0]-$leading,
            75,$topbot[1]-$leading, 340,$topbot[1]-$leading,
            75,$topbot[0]-2*$leading, 338,$topbot[0]-2*$leading,
            75,$topbot[1]-2*$leading, 338,$topbot[1]-2*$leading];
$ant6->markup($note, $corners, "StrikeOut", -opacity=>0.5);

# ------------------
$pdf->saveas("$0.pdf");
$pdf->end();

exit;

sub y_topbot {
    my ($topbase, $line, $leading, $fontsize, $descender) = @_;
    # topbase = y of top line's baseline
    # line = line number (top line is 0, below it is 1, etc.)
    # leading = baseline-to-baseline distance
    # fontsize = font size (baseline to top of ascenders)
    # descender = descender size
    my $base = $topbase - $line * $leading;
    my $delta = $fontsize/20;
    return ($base+$fontsize-$delta, $base-$descender-$delta);
}

__END__