19
19
20
20
var ANNOT_MIN_SIZE = 10 ; // px
21
21
22
- var AnnotationUtils = ( function AnnotationUtilsClosure ( ) {
22
+ var AnnotationLayer = ( function AnnotationLayerClosure ( ) {
23
23
// TODO(mack): This dupes some of the logic in CanvasGraphics.setFont()
24
24
function setTextStyles ( element , item , fontObj ) {
25
-
26
25
var style = element . style ;
27
26
style . fontSize = item . fontSize + 'px' ;
28
27
style . direction = item . fontDirection < 0 ? 'rtl' : 'ltr' ;
@@ -43,34 +42,43 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
43
42
style . fontFamily = fontFamily + fallbackName ;
44
43
}
45
44
46
- function initContainer ( item ) {
45
+ function getContainer ( data , page , viewport ) {
47
46
var container = document . createElement ( 'section' ) ;
48
- var cstyle = container . style ;
49
- var width = item . rect [ 2 ] - item . rect [ 0 ] ;
50
- var height = item . rect [ 3 ] - item . rect [ 1 ] ;
51
-
52
- // Border
53
- if ( item . borderStyle . width > 0 ) {
54
- // Border width
55
- container . style . borderWidth = item . borderStyle . width + 'px' ;
56
- if ( item . borderStyle . style !== AnnotationBorderStyleType . UNDERLINE ) {
47
+ var width = data . rect [ 2 ] - data . rect [ 0 ] ;
48
+ var height = data . rect [ 3 ] - data . rect [ 1 ] ;
49
+
50
+ container . setAttribute ( 'data-annotation-id' , data . id ) ;
51
+
52
+ data . rect = Util . normalizeRect ( [
53
+ data . rect [ 0 ] ,
54
+ page . view [ 3 ] - data . rect [ 1 ] + page . view [ 1 ] ,
55
+ data . rect [ 2 ] ,
56
+ page . view [ 3 ] - data . rect [ 3 ] + page . view [ 1 ]
57
+ ] ) ;
58
+
59
+ CustomStyle . setProp ( 'transform' , container ,
60
+ 'matrix(' + viewport . transform . join ( ',' ) + ')' ) ;
61
+ CustomStyle . setProp ( 'transformOrigin' , container ,
62
+ - data . rect [ 0 ] + 'px ' + - data . rect [ 1 ] + 'px' ) ;
63
+
64
+ if ( data . borderStyle . width > 0 ) {
65
+ container . style . borderWidth = data . borderStyle . width + 'px' ;
66
+ if ( data . borderStyle . style !== AnnotationBorderStyleType . UNDERLINE ) {
57
67
// Underline styles only have a bottom border, so we do not need
58
68
// to adjust for all borders. This yields a similar result as
59
69
// Adobe Acrobat/Reader.
60
- width = width - 2 * item . borderStyle . width ;
61
- height = height - 2 * item . borderStyle . width ;
70
+ width = width - 2 * data . borderStyle . width ;
71
+ height = height - 2 * data . borderStyle . width ;
62
72
}
63
73
64
- // Horizontal and vertical border radius
65
- var horizontalRadius = item . borderStyle . horizontalCornerRadius ;
66
- var verticalRadius = item . borderStyle . verticalCornerRadius ;
74
+ var horizontalRadius = data . borderStyle . horizontalCornerRadius ;
75
+ var verticalRadius = data . borderStyle . verticalCornerRadius ;
67
76
if ( horizontalRadius > 0 || verticalRadius > 0 ) {
68
77
var radius = horizontalRadius + 'px / ' + verticalRadius + 'px' ;
69
78
CustomStyle . setProp ( 'borderRadius' , container , radius ) ;
70
79
}
71
80
72
- // Border style
73
- switch ( item . borderStyle . style ) {
81
+ switch ( data . borderStyle . style ) {
74
82
case AnnotationBorderStyleType . SOLID :
75
83
container . style . borderStyle = 'solid' ;
76
84
break ;
@@ -95,24 +103,27 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
95
103
break ;
96
104
}
97
105
98
- // Border color
99
- if ( item . color ) {
106
+ if ( data . color ) {
100
107
container . style . borderColor =
101
- Util . makeCssRgb ( item . color [ 0 ] | 0 ,
102
- item . color [ 1 ] | 0 ,
103
- item . color [ 2 ] | 0 ) ;
108
+ Util . makeCssRgb ( data . color [ 0 ] | 0 ,
109
+ data . color [ 1 ] | 0 ,
110
+ data . color [ 2 ] | 0 ) ;
104
111
} else {
105
112
// Transparent (invisible) border, so do not draw it at all.
106
113
container . style . borderWidth = 0 ;
107
114
}
108
115
}
109
116
110
- cstyle . width = width + 'px' ;
111
- cstyle . height = height + 'px' ;
117
+ container . style . left = data . rect [ 0 ] + 'px' ;
118
+ container . style . top = data . rect [ 1 ] + 'px' ;
119
+
120
+ container . style . width = width + 'px' ;
121
+ container . style . height = height + 'px' ;
122
+
112
123
return container ;
113
124
}
114
125
115
- function getHtmlElementForTextWidgetAnnotation ( item , commonObjs ) {
126
+ function getHtmlElementForTextWidgetAnnotation ( item , page ) {
116
127
var element = document . createElement ( 'div' ) ;
117
128
var width = item . rect [ 2 ] - item . rect [ 0 ] ;
118
129
var height = item . rect [ 3 ] - item . rect [ 1 ] ;
@@ -128,15 +139,15 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
128
139
content . style . display = 'table-cell' ;
129
140
130
141
var fontObj = item . fontRefName ?
131
- commonObjs . getData ( item . fontRefName ) : null ;
142
+ page . commonObjs . getData ( item . fontRefName ) : null ;
132
143
setTextStyles ( content , item , fontObj ) ;
133
144
134
145
element . appendChild ( content ) ;
135
146
136
147
return element ;
137
148
}
138
149
139
- function getHtmlElementForTextAnnotation ( item ) {
150
+ function getHtmlElementForTextAnnotation ( item , page , viewport ) {
140
151
var rect = item . rect ;
141
152
142
153
// sanity check because of OOo-generated PDFs
@@ -147,7 +158,7 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
147
158
rect [ 2 ] = rect [ 0 ] + ( rect [ 3 ] - rect [ 1 ] ) ; // make it square
148
159
}
149
160
150
- var container = initContainer ( item ) ;
161
+ var container = getContainer ( item , page , viewport ) ;
151
162
container . className = 'annotText' ;
152
163
153
164
var image = document . createElement ( 'img' ) ;
@@ -253,8 +264,30 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
253
264
return container ;
254
265
}
255
266
256
- function getHtmlElementForLinkAnnotation ( item ) {
257
- var container = initContainer ( item ) ;
267
+ function getHtmlElementForLinkAnnotation ( item , page , viewport , linkService ) {
268
+ function bindLink ( link , dest ) {
269
+ link . href = linkService . getDestinationHash ( dest ) ;
270
+ link . onclick = function annotationsLayerBuilderLinksOnclick ( ) {
271
+ if ( dest ) {
272
+ linkService . navigateTo ( dest ) ;
273
+ }
274
+ return false ;
275
+ } ;
276
+ if ( dest ) {
277
+ link . className = 'internalLink' ;
278
+ }
279
+ }
280
+
281
+ function bindNamedAction ( link , action ) {
282
+ link . href = linkService . getAnchorUrl ( '' ) ;
283
+ link . onclick = function annotationsLayerBuilderNamedActionOnClick ( ) {
284
+ linkService . executeNamedAction ( action ) ;
285
+ return false ;
286
+ } ;
287
+ link . className = 'internalLink' ;
288
+ }
289
+
290
+ var container = getContainer ( item , page , viewport ) ;
258
291
container . className = 'annotLink' ;
259
292
260
293
var link = document . createElement ( 'a' ) ;
@@ -264,26 +297,62 @@ var AnnotationUtils = (function AnnotationUtilsClosure() {
264
297
link . target = LinkTargetStringMap [ PDFJS . externalLinkTarget ] ;
265
298
}
266
299
300
+ if ( ! item . url ) {
301
+ if ( item . action ) {
302
+ bindNamedAction ( link , item . action ) ;
303
+ } else {
304
+ bindLink ( link , ( 'dest' in item ) ? item . dest : null ) ;
305
+ }
306
+ }
307
+
267
308
container . appendChild ( link ) ;
268
309
269
310
return container ;
270
311
}
271
312
272
- function getHtmlElement ( data , objs ) {
313
+ function getHtmlElement ( data , page , viewport , linkService ) {
273
314
switch ( data . annotationType ) {
274
315
case AnnotationType . WIDGET :
275
- return getHtmlElementForTextWidgetAnnotation ( data , objs ) ;
316
+ return getHtmlElementForTextWidgetAnnotation ( data , page ) ;
276
317
case AnnotationType . TEXT :
277
- return getHtmlElementForTextAnnotation ( data ) ;
318
+ return getHtmlElementForTextAnnotation ( data , page , viewport ) ;
278
319
case AnnotationType . LINK :
279
- return getHtmlElementForLinkAnnotation ( data ) ;
320
+ return getHtmlElementForLinkAnnotation ( data , page , viewport ,
321
+ linkService ) ;
280
322
default :
281
323
throw new Error ( 'Unsupported annotationType: ' + data . annotationType ) ;
282
324
}
283
325
}
284
326
327
+ function render ( viewport , div , annotations , page , linkService ) {
328
+ for ( var i = 0 , ii = annotations . length ; i < ii ; i ++ ) {
329
+ var data = annotations [ i ] ;
330
+ if ( ! data || ! data . hasHtml ) {
331
+ continue ;
332
+ }
333
+
334
+ var element = getHtmlElement ( data , page , viewport , linkService ) ;
335
+ div . appendChild ( element ) ;
336
+ }
337
+ }
338
+
339
+ function update ( viewport , div , annotations ) {
340
+ for ( var i = 0 , ii = annotations . length ; i < ii ; i ++ ) {
341
+ var data = annotations [ i ] ;
342
+ var element = div . querySelector (
343
+ '[data-annotation-id="' + data . id + '"]' ) ;
344
+ if ( element ) {
345
+ CustomStyle . setProp ( 'transform' , element ,
346
+ 'matrix(' + viewport . transform . join ( ',' ) + ')' ) ;
347
+ }
348
+ }
349
+ div . removeAttribute ( 'hidden' ) ;
350
+ }
351
+
285
352
return {
286
- getHtmlElement : getHtmlElement
353
+ render : render ,
354
+ update : update
287
355
} ;
288
356
} ) ( ) ;
289
- PDFJS . AnnotationUtils = AnnotationUtils ;
357
+
358
+ PDFJS . AnnotationLayer = AnnotationLayer ;
0 commit comments